Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
  1. #21
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Looks like a space between tokens
    Why can't I do

    Scanner file = new Scanner (new File("prog602.dat"));
    String [] tokens = file.split(" ");

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Read the API doc for the Scanner class and see what methods it has and how to use them.
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Read the API doc for the Scanner class and see what methods it has and how to use them.
    Okay, i'm really lost now. Sorry

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Do it in two steps:
    1) read a line
    2) split the line into an array.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Do it in two steps:
    1) read a line
    2) split the line into an array.
    Yeah, but I can't get it to read the first line alone.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Post your code and its output that show what is happening.

    what do you mean by: read the first line alone.
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class prog602 {
    	public static void main (String [] args)throws IOException{
    		Scanner file = new Scanner (new File("prog602.dat"));
    		
    		int val = file.nextInt();
    		file.nextLine();
    		
    		for(int i = 0;i<val;i++)
    		{
    			int num = file.nextInt();
    			System.out.println(nums);
    		}
    			
    	}
    }
    Output:
    564
    33
    654
    8321
    15
    10
    225
    Sample data:

    7
    564 33 654 8321 15
    10 225 4557 254
    22 39 22 45 33 11 19
    1 2 3 4 5 6 7
    -10 9 1 8 -999
    -1 2 3 4 5 6 7
    0 34 45 23 12
    Last edited by jsoeung; 04-18-2012 at 04:04 AM.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    The nextInt() method reads the next token and converts it to an int value. You need to use the nextLine() method to read a line. The code is ignoring the String returned by the nextLine() method. It should assign it to a String variable.
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    The nextInt() method reads the next token and converts it to an int value. You need to use the nextLine() method to read a line. The code is ignoring the String returned by the nextLine() method. It should assign it to a String variable.
    How do I stop the for loop then without an int? What do I put in place of i<val; in the for loop?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Your code reads the number of lines from the file ok using nextInt(). After that read lines (not ints) in the loop as it is now written.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Your code reads the number of lines from the file ok using nextInt(). After that read lines (not ints) in the loop as it is now written.
    Ohhhhh awesome! Now it is printing the data just how it is displayed in the file. Should the split method be inside the for loop?

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Use the split method on the lines you want to split, where you want to split them.

    That's it for tonight. See you later.
    If you don't understand my response, don't ignore it, ask a question.

  13. #33
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Use the split method on the lines you want to split, where you want to split them.
    How do I get the largest int value out of a string array?

    That's it for tonight. See you later.
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class prog602 {
    	public static void main (String [] args)throws IOException{
    		Scanner file = new Scanner (new File("prog602.dat"));
    		int val = file.nextInt();
    		file.nextLine();
    		for(int i = 0;i<val;i++)
    		{
    			String str = file.nextLine();
    			String[] tokens= str.split(" ");
    			int largest = 0;
    			for (int j = 0;j<tokens.length;j++)
    			{
    				int num = tokens[j]; //won't let me store it into a num so I can compare it to largest/
    System.out.print(tokens[j]);
    				System.out.print(" ");
    			}	
    System.out.println("largest- "+largest);		
    		}
    	}
    }

  14. #34
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    How about using the following on line 15:

    Java Code:
    int num = Integer.parseInt(tokens[j]);

  15. #35
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    I got it! Thanks guys.
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class prog602 {
        public static void main (String [] args)throws IOException{
            Scanner file = new Scanner (new File("prog602.dat"));
            int val = file.nextInt();
            file.nextLine();
            for(int i = 0;i<val;i++)
            {
                String str = file.nextLine();
                String[] tokens= str.split(" ");
                int largest = 0;
                for (int j = 0;j<tokens.length;j++)
                {
                    int num = Integer.parseInt(tokens[j]); 
                    	if(num>largest){
                    		largest =num;
                    	}
                    System.out.print(tokens[j]);
                    System.out.print(" ");
                }   
    System.out.println("largest- "+largest);        
            }
        }
    }

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Congratulatons
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 11-21-2011, 11:09 AM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. Line Reader changeing to read 1 line
    By Javanooby in forum New To Java
    Replies: 15
    Last Post: 05-10-2011, 04:34 AM
  4. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •