codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Help with arrays  gosi at 21:17 on Tuesday, December 20, 2005
 

I´m now figthing with arrays, and not doing too well. I have to read numbers from a file where the values are f.ex.

nr a b weight
01 10 15 50
02 08 03 20

What I need to do is to let another class know which a,b and weight values belong to each number for further calculations.
I dont think that I´m getting the correct output from the array.



import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;



public class CP extends JPanel{

static String file;
static FileWriter fw;
static BufferedWriter bw;
static PrintWriter outFile;

private ArrayList arrayInfo = new ArrayList();

public CP() throws IOException
{
file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
}
public void openCommand(File file)
{
arrayInfo.clear();
try
{

long length = file.length();
FileReader reader = new FileReader(file);
String temp = Long.toString(length);
int size = Integer.parseInt(temp);
char[] table = new char[size];
ArrayList list = new ArrayList();
String count = "";
try
{
reader.read(table);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"Can´t read the file", "Error", JOptionPane.ERROR_MESSAGE);
file=null;
}
for(int i=0; i<table.length;i++)
{
if(table !='\n')
{
if(table =='r')
{
count += " ";
}
else if(table== ',')
{
count += " ";
}
else
{
count += table;
list.add(table);
}
}
}
String[] arr = count.split(" ");
int[] numb = new int[arr.length];

for(int n=0; n<arr.length; n++)
{
numb[n]= Integer.parseInt(arr[n]);
}
for(int m=0; m<numb.length;m++)
{
int nr,a,b,weight;

nr = numb[m];
m++;
a = numb[m];
m++;
b = numb[m];
}}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
}
}



  Re: Help with arrays  crwood at 04:50 on Wednesday, December 21, 2005
 


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;

public class CP extends JPanel {

String file;
FileWriter fw;
BufferedWriter bw;
PrintWriter outFile;
int[] aVals;
int[] bVals;
int[] weights;

private ArrayList arrayInfo = new ArrayList();

public CP()
{
//file = "/forum/results.txt";
//fw = new FileWriter(file);
//bw = new BufferedWriter(fw);
//outFile = new PrintWriter(bw);
openCommand(new File("/forum/cp.txt"));
}

public void openCommand(File file)
{
arrayInfo.clear();
StringBuffer sb = new StringBuffer();
try
{
BufferedReader reader = new BufferedReader(
new FileReader(file));
// read in all the data from file
String line = "";
while((line = reader.readLine()) != null)
sb.append(line + "/index.html");
reader.close();
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Can´t read the file", "Error",
JOptionPane.ERROR_MESSAGE);
file=null;
}
// recover and parse data
String[] lines = sb.toString().split("/index.html");
// now that we know the number of lines we can
// initialize the arrays that will hold the parsed data
aVals = new int[lines.length];
bVals = new int[lines.length];
weights = new int[lines.length];
// just for fun let's pass on the data to instances
// of a data store class - OtherClass
OtherClass[] otherClasses = new OtherClass[lines.length];
for(int j = 0; j < lines.length; j++)
{
String[] vals = lines[j].split("\\s");
// skip the first token (vals[0]), a two-digit number
int a = Integer.parseInt(vals[1]);
int b = Integer.parseInt(vals[2]);
int w = Integer.parseInt(vals[3]);
// assign j_th element of our three arrays
aVals[j] = a;
bVals[j] = b;
weights[j] = w;
// assign j_th element of our OtherClass array
otherClasses[j] = new OtherClass(a, b, w);
}
// print out our three arrays; for j2se 1.5 see:
// java.util.Arrays.toString(int[] a)
print(aVals, "aVals");
print(bVals, "bVals");
print(weights, "weights");
// print out the OtherClass array elements
for(int j = 0; j < otherClasses.length; j++)
System.out.println("otherClasses[" + j + "] = " + otherClasses[j]);
}

private static void print(int[] array, String arrayName)
{
System.out.print(arrayName + " = [");
for(int j = 0; j < array.length; j++)
{
System.out.print(array[j]);
if(j < array.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}

public static void main(String[] args)
{
new CP();
}
}

class OtherClass
{
int a;
int b;
int weight;

public OtherClass(int a, int b, int weight)
{
this.a = a;
this.b = b;
this.weight = weight;
}

public int getA() { return a; }
public int getB() { return b; }
public int getWeight() { return weight; }

public String toString()
{
return "OtherClass[a:" + a + ", b:" + b + ", weight:" + weight + "]";
}
}

cp.txt

01 10 15 50
02 08 03 20









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005