|
||
|
|||
|
Hello,
I am trying to read a simple .txt file of ints into a 2D array. However, the scanner object seems to skip the first few ints. The "scanner.hasNext()" seems to advance the scanner. Please let me know what I am doing wrong. I attached the file "input.txt" Thanks Java Code
public int[][] getMatrix(String fileName)
{
int totalrows = 0;
int totalcols= 0;
int[][] matrix = null;
File file = null;
Scanner scanner = null;
int rowIndex = 0;
int colIndex = 0;
try
{
scanner =new Scanner(new File("input.txt"));
}
catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println ("File not found!");
// Stop program if no file found
System.exit (0);
}
while (scanner.hasNext())
{
if (scanner.hasNextInt())
totalrows = scanner.nextInt();
if (scanner.hasNextInt())
totalcols = scanner.nextInt();
else
scanner.next();
}
matrix = new int[totalrows][totalcols];
while (true)
{
if (!scanner.hasNextInt() || !(scanner.next() == "\r") || !(scanner.next() == " "))
break;
matrix[rowIndex][colIndex] = scanner.nextInt();
rowIndex++;
if (scanner.next() == "\r")
{
rowIndex = 0;
colIndex++;
}
}
return matrix;
}
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read data from text file | yroll | Algorithms & Recursion | 4 | 31-12-2009 05:40 AM |
| How to Read the last line of a file | JavaPF | Java Code Snippets and Tutorials | 2 | 10-09-2009 07:26 AM |
| How to Read a Portion of a File | jazz2k8 | File I/O & Other I/O Streams | 3 | 07-07-2009 09:16 PM |
| exception while Read very large file > 300 MB | ps.ganesh | File I/O & Other I/O Streams | 2 | 12-06-2009 04:39 AM |
| How to Read a file line by line - java.io.BufferedReader | JavaPF | Java Code Snippets and Tutorials | 0 | 19-05-2008 11:32 AM |
|
100 most searched terms
Search Cloud
|
| 2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? |