| | |
Can someone tell me what i am doing wrong?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
hi,
I am trying to create a 2 dimmensional array that reads data from a text file using a buffer reader and tokenizer to separate the information in the text file. As far as using the buffer reader to read from text file it seems to work but when i try and introduce a tokenizer to read tokens i keep getting errors. Also i wanted it to display the data within the array eg:
file coordinates x1,y1 = 0 [101][103]
x2,y2 = 1 [107][109]
and so on...
here is what is in the text file:
101 103
107 109
113 127
131 137
139 149
151 157
163 167
173 179
181 191
193 197
and this is the snippet of the code I am working on:
import java.io.*;
import java.util.*;
import java.lang.Math;
class try1
{
public static void print (int[][] array,int width)
{ int nrows = array.length;
int ncols = array[0].length;
for (int i=0; i<nrows; i++)
{
for (int j=0; j<ncols; j++)
{
String outputString = Integer.toString(array[i][j]);
int length = outputString.length();
if (length < width)
for (int k = length; k < width; k++)
System.out.print(" ");
System.out.print(outputString + "\t");
}
System.out.println(" ");
}
}
public static void main (String[]args)throws IOException
{
String text;
try
{
BufferedReader br = new BufferedReader(new FileReader("ex1.txt"));
int counter, numberOfTokens = 20, number;
while ((text = br.readLine()) != null)
{
System.out.println(text);
}
// Always close a file after opening.
br.close();
}
// Catch and handle the exception in the // event that the named file cannot be found.
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
return;
}
// Catch and handle an exception if there is // a problem reading the file.
catch (IOException ioe)
{
System.out.println(ioe);
}
}
StringTokenizer tokenizer = new StringTokenizer(br.readLine));
numberOfTokens = tokenizer.countTokens();
while (numberOfTokens != 0)
{
for (counter=0; counter < numberOfTokens; counter++)
{
number = new Integer(tokenizer.nextToken()).intValue();number++;
System.out.print(number + " ");
}
System.out.println();
tokenizer = new StringTokenizer(br.readLine());
numberOfTokens = tokenizer.countTokens();
}
}
I am trying to create a 2 dimmensional array that reads data from a text file using a buffer reader and tokenizer to separate the information in the text file. As far as using the buffer reader to read from text file it seems to work but when i try and introduce a tokenizer to read tokens i keep getting errors. Also i wanted it to display the data within the array eg:
file coordinates x1,y1 = 0 [101][103]
x2,y2 = 1 [107][109]
and so on...
here is what is in the text file:
101 103
107 109
113 127
131 137
139 149
151 157
163 167
173 179
181 191
193 197
and this is the snippet of the code I am working on:
import java.io.*;
import java.util.*;
import java.lang.Math;
class try1
{
public static void print (int[][] array,int width)
{ int nrows = array.length;
int ncols = array[0].length;
for (int i=0; i<nrows; i++)
{
for (int j=0; j<ncols; j++)
{
String outputString = Integer.toString(array[i][j]);
int length = outputString.length();
if (length < width)
for (int k = length; k < width; k++)
System.out.print(" ");
System.out.print(outputString + "\t");
}
System.out.println(" ");
}
}
public static void main (String[]args)throws IOException
{
String text;
try
{
BufferedReader br = new BufferedReader(new FileReader("ex1.txt"));
int counter, numberOfTokens = 20, number;
while ((text = br.readLine()) != null)
{
System.out.println(text);
}
// Always close a file after opening.
br.close();
}
// Catch and handle the exception in the // event that the named file cannot be found.
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
return;
}
// Catch and handle an exception if there is // a problem reading the file.
catch (IOException ioe)
{
System.out.println(ioe);
}
}
StringTokenizer tokenizer = new StringTokenizer(br.readLine));
numberOfTokens = tokenizer.countTokens();
while (numberOfTokens != 0)
{
for (counter=0; counter < numberOfTokens; counter++)
{
number = new Integer(tokenizer.nextToken()).intValue();number++;
System.out.print(number + " ");
}
System.out.println();
tokenizer = new StringTokenizer(br.readLine());
numberOfTokens = tokenizer.countTokens();
}
}
What error are you getting?
I strongly recommend you to reorganize the StringToken section of code I've attached below because it is easy to make a logical error the way you have coded it. It is better to take this form instead:
// See the Java Almanac entry here
I strongly recommend you to reorganize the StringToken section of code I've attached below because it is easy to make a logical error the way you have coded it. It is better to take this form instead:
// See the Java Almanac entry here
Java Syntax (Toggle Plain Text)
String aString = "word1 word2 word3"; StringTokenizer parser = new StringTokenizer(aString); while (parser.hasMoreTokens()) { processWord(parser.nextToken()); }
•
•
•
•
StringTokenizer tokenizer = new StringTokenizer(br.readLine));
numberOfTokens = tokenizer.countTokens();
while (numberOfTokens != 0)
{
for (counter=0; counter < numberOfTokens; counter++)
{
number = new Integer(tokenizer.nextToken()).intValue();number++;
System.out.print(number + " ");
}
System.out.println();
tokenizer = new StringTokenizer(br.readLine());
numberOfTokens = tokenizer.countTokens();
}
}
•
•
•
•
In a world without walls or fences,
What use are Windows and Gates.
![]() |
Similar Threads
- My CD-RW plays but won't burn. What could be wrong?? Help Please? (Storage)
- Retrieve email I sent to the wrong person (Web Browsers)
- Ram voltage wrong?? (Motherboards, CPUs and RAM)
- Am I going about this the wrong way (IT Professionals' Lounge)
- wats wrong with imesh??? (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Countdown
- Next Thread: Wait while processing Note
Views: 2591 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation binary bluetooth chat chooser class classes client code component converter database detection digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle os pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows





