Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: New person Just trying to read a file of ints

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

     
    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 Attached Files


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    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.

    for (int i = 0; i < totalrows; i++)
    {
         for (int j = 0; j < totalcols; j++)
         {
              matrix[i][j] = scanner.nextInt();
         }
    }

Similar Threads

  1. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 12:40 AM
  2. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  3. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM
  4. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 11th, 2009, 11:39 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM