Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Help with 'while' loop

    Hey all, I'm new to these forums and new to java. I've just started my first year at university, and need a little help with a 'while' loop.

    I'll show you what I've wrote below, and hopefully someone may be able to tell me where I've gone wrong with the 'while' loop.

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class test{
    	public static void main (String args[])throws Exception{
    		File myFile = new File("alice.txt");  
    		Scanner fileScan = new Scanner(myFile);  
    	
    		String[] line = new String[20];
    
               while (fileScan.hasNext()) {
               line = fileScan.nextLine();
               array[count] = line;
               count++;
               }
                    for (String num : line){
    	        System.out.println(num);
                    }
    
    fileScan.close();
    
    	}
    }
    The

    Java Code:
     while (fileScan.hasNext()) {
               line = fileScan.nextLine();
               array[count] = line;
               count++;
               }
    is incorrect.. any help appreciated! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One issue is that your hasNext should match your next. You're checking hasNext() and then calling nextLine and the two don't match. In other words, if you are checking for next int, then you should use hasNextInt and inside the loop call nextInt. Same for hasNextDouble/nextDouble, hasNextLine/nextLine, and hasNext/next.

    I suggest you check hasNextLine() and inside the loop use nextLine.

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi,

    Have you tried to compile this class? If you did the error messages should already have given some ideas of what is wrong with your code.

    • line is declared an array so if you want to store one line in the array you have to provide an index: line[count] = filescan.nextLine();
    • array has not been declared but you have no longer need for it either.
    • count has not been declared and remember to initialise it after declaration.


    There is a functional errors in your code as well which the compiler cannot tell you. Have a good look at the Scanner methods you use. They do not match.

    I hope this helps,
    Good luck!

    Oh well, Fubarable made a comment about the scanner methods already...
    Last edited by venerik; 11-09-2010 at 11:04 PM. Reason: Fubarable was quicker to respond...
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Am I any closer?

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class test{
    	public static void main (String args[])throws Exception{
    		File myFile = new File("alice.txt");  
    		Scanner fileScan = new Scanner(myFile);  
    	
    		String[] line = new String[20];
    
     while (fileScan.hasNextLine()) {
               line[count] = fileScan.nextLine();
               count++;
               }
               while (count <= 20);
    for (String num : line){
    	System.out.println(num);
    }
    fileScan.close();
    	}
    }
    My head is starting to hurt now... :p

    Errors I'm getting are

    Java Code:
    test.java:12: cannot find symbol
    symbol  : variable count
    location: class test
               line[count] = fileScan. nextLine();
                    ^
    test.java:13: cannot find symbol
    symbol  : variable count
    location: class test
               count++;
               ^
    test.java:15: cannot find symbol
    symbol  : variable count
    location: class test
               while (count <= 20);
                      ^
    Last edited by ls7897; 11-09-2010 at 11:24 PM.

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Re-read my previous reply (especially the third bullet :))
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    I have no idea how declare and initialise the count..... :confused:
    I started Java about 3 weeks ago and am learning very slowly... :p

  7. #7
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Ok, I know i'm being a pain, but I think this is what you mean...

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class test{
    	public static void main (String args[])throws Exception{
    		File myFile = new File("alice.txt");  
    		Scanner fileScan = new Scanner(myFile);  
    	
    		String[] line = new String[20];
    		int count = 0;
     while (fileScan.hasNextLine()) {
               line[count] = fileScan. nextLine();
               count++;
               }
    
    for (String num : line){
    	System.out.println(num);
    }
    fileScan.close();
    	}
    }
    It compiles, but I get error...

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    	at test.main(test.java:12)

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    String[] line = new String[20];
    I think the line of the file that you load is greater than the lenght of your array.

  9. #9
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Sorry for causing any pain... while I was writing my first reply I considered to warn you for this but somehow I didn't.

    The error occurs because the program tries to access the array element with index 20. The size of your array is 20 meaning the highest accessible index is 19 (the index starts with 0).

    I leave it up to you to come up with a solution to prevent this error...

    Success!
    Erik.
    I'm new to Java but I like to help where ever I can. :)

  10. #10
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Ok, I've added while (count<=19);

    BUT

    it compiles fine, when I run it, nothing happens, terminal just goes blank.....?

    I know I'm being a pain, but I really appreciate it!

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) we don't know the context of your current problem. Where did you add this line of code?
    2) Before coming here with every error immediately as it occurs, you will want to learn debugging techniques to help you learn to fix it yourself. For instance you'll probably want to sprinkle your code with println statements so you can see what is happening at each step of your code.

  12. #12
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    class test{
    	public static void main (String args[])throws Exception{
    	File myFile = new File("alice.txt");  
    	Scanner fileScan = new Scanner(myFile);  
    	
    	String[] line = new String[20];
    	int count = 1;
     
     	while (fileScan.hasNextLine()) {
        line[count] = fileScan. nextLine();
        count++;
        while (count<=5);
    	}
    	for (String num : line){
    	System.out.println(num);
    	}
    fileScan.close();
    	}
    }
    This is the source code. It compiles, but when I run it, the cursor just blinks and nothing happens...
    Last edited by ls7897; 11-10-2010 at 01:16 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This line makes no sense:
    Java Code:
     while (count<=5);
    It is equivalent to this bit of code:
    Java Code:
    while (count <= 5) {
      ;
    }

    Since count does not change in this loop, it will loop for ever. Get rid of that line.

  14. #14
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This line makes no sense:
    Java Code:
     while (count<=5);
    It is equivalent to this bit of code:
    Java Code:
    while (count <= 5) {
      ;
    }

    Since count does not change in this loop, it will loop for ever. Get rid of that line.
    I changed the code to what you said, but I cannot delete it as there is over 20 lines in the text document it is reading and if I get rid of while
    (count <= 5) {
    ;
    }
    then it will not compile as there's more lines then the array... ?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ls7897 View Post
    I changed the code to what you said,
    Changed it how?

    but I cannot delete it as there is over 20 lines in the text document it is reading and if I get rid of while
    (count <= 5) {
    ;
    }
    then it will not compile as there's more lines then the array... ?
    But that line serves no useful purpose other than to cause an infinite loop. Surely that's not what you want there is it?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, please please fix your code's indentation. It is very difficult to read with its current random indents.

  17. #17
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Changed it how?



    But that line serves no useful purpose other than to cause an infinite loop. Surely that's not what you want there is it?
    Ok, so I delete that line all together and change the array to 609 (because there is 609 lines in the text document).
    How do I print just 20 lines?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, I wonder if Java is looking for your alice.txt file where you think it is.

    To find out where Java is looking, put a debug System.out.println statement in your code (see I told you they'd be handy!), something like this:

    Java Code:
          File myFile = new File("alice.txt");
          System.out.println(myFile.getAbsolutePath()); // [color="red"]add this line![/color]

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ls7897 View Post
    Ok, so I delete that line all together and change the array to 609 (because there is 609 lines in the text document).
    How do I print just 20 lines?
    What's the standard way to loop 20 times?

  20. #20
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  2. do while loop
    By sr20guy in forum New To Java
    Replies: 16
    Last Post: 04-08-2010, 03:59 AM
  3. need a loop to add new row(s)
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 06:21 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. Loop, is it what I need?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:28 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
  •