Results 1 to 20 of 28
Thread: Help with 'while' loop
- 11-09-2010, 10:39 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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.
TheJava 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(); } }
is incorrect.. any help appreciated! :)Java Code:while (fileScan.hasNext()) { line = fileScan.nextLine(); array[count] = line; count++; }
-
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.
- 11-09-2010, 11:02 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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. :)
- 11-09-2010, 11:21 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
Am I any closer?
My head is starting to hurt now... :pJava 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(); } }
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.
- 11-09-2010, 11:32 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Re-read my previous reply (especially the third bullet :))
I'm new to Java but I like to help where ever I can. :)
- 11-09-2010, 11:34 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
I have no idea how declare and initialise the count..... :confused:
I started Java about 3 weeks ago and am learning very slowly... :p
- 11-09-2010, 11:41 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
Ok, I know i'm being a pain, but I think this is what you mean...
It compiles, but I get error...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(); } }
Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at test.main(test.java:12)
- 11-10-2010, 12:21 AM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I think the line of the file that you load is greater than the lenght of your array.String[] line = new String[20];
- 11-10-2010, 12:25 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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. :)
- 11-10-2010, 12:34 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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!
-
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.
- 11-10-2010, 01:12 AM #12
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
This is the source code. It compiles, but when I run it, the cursor just blinks and nothing happens...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(); } }Last edited by ls7897; 11-10-2010 at 01:16 AM.
-
This line makes no sense:
It is equivalent to this bit of code:Java Code:while (count<=5);
Java Code:while (count <= 5) { ; }
Since count does not change in this loop, it will loop for ever. Get rid of that line.
- 11-10-2010, 01:24 AM #14
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
-
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?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... ?
-
Also, please please fix your code's indentation. It is very difficult to read with its current random indents.
- 11-10-2010, 01:31 AM #17
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
-
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]
-
- 11-10-2010, 01:36 AM #20
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
do while loop
By sr20guy in forum New To JavaReplies: 16Last Post: 04-08-2010, 03:59 AM -
need a loop to add new row(s)
By doha786 in forum New To JavaReplies: 1Last Post: 02-03-2010, 06:21 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Loop, is it what I need?
By dbashby in forum New To JavaReplies: 4Last Post: 04-14-2009, 04:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks