Go Back   Java Programming Forums > Java Standard Edition Programming Help > File I/O & Other I/O Streams


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2010, 09:41 PM
Junior Member
 

Join Date: Mar 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
dubois.ford is on a distinguished road
Default New person Just trying to read a file of ints

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;



}
Attached Files
File Type: txt input.txt (40 Bytes, 2 views)



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 08-03-2010, 03:47 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: New person Just trying to read a file of ints

The hasNext method doesn't advance the read point. However, in the point where you're reading in data, you use the next() method, which will advance the read point.

A suggestion would be to simply read in the number of integers that should be in that file.

Java Code
for (int i = 0; i < totalrows; i++)
{
     for (int j = 0; j < totalcols; j++)
     {
          matrix[i][j] = scanner.nextInt();
     }
}
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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?

All times are GMT. The time now is 01:53 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.