Results 1 to 10 of 10
Thread: Help with loading
- 08-06-2012, 01:44 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Help with loading
I'm having trouble doing a load that i thought to be pretty simple. I've checked the code and i don't know what is going wrong.
here's the code:
The problem i'm having is that for some reason even when the file i'm reading from has true in that line, it still saves the boolean as false sometimes, and vice-versa. For instance, my sample files first 6 lines are true, true, true, false, false, false but the array reads true, true, true, false, false, true. The last false is read as true! Does anyone know why this might be?Java Code:public boolean[] Load(String file) { boolean[] fill = new boolean[288]; int count = 0; try { Scanner loader = new Scanner(new File(file)); while(loader.hasNext()) { if(loader.nextLine().equals("true")) { fill[count] = true; } else if(loader.nextLine().equals("false")) { fill[count] = false; } count++; } loader.close(); }catch(IOException e){} return fill; }
- 08-06-2012, 04:29 AM #2
Re: Help with loading
How are you running that code?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-06-2012, 04:47 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Re: Help with loading
What do you mean? I just pasted code for that one method. Um, i call it after the user presses a JButton if that's what you mean.If you let me know what you want to know more specifically ill be glad to help. I should add that i've done some more testing and found that the Scanner seems to find something else other than true or false in the line, but looking at the file that's impossible. The only things in that file are true or false and no double spacing or anything. I'm not really sure what that means.
- 08-06-2012, 05:49 AM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-06-2012, 06:54 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Re: Help with loading
Oh, well i'm using an IDE, JCreator to be more exact.
- 08-06-2012, 09:27 AM #6
Re: Help with loading
Well, I've never seen JCreator but if it has a build process anything like NetBeans, you may be inadvertently accessing an old version of the file.
Is the String parameter an absolute path or relative?
If that's not it, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem, along with a sample text file.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-06-2012, 10:40 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Help with loading
You're reading the line twice if nextLine() returns "false".
So store the String returned by nextLine() into a variable then test the variable. Don't read and test in one go.Java Code:if(loader.nextLine().equals("true")) // Reads a line from the Scanner. { fill[count] = true; } else if(loader.nextLine().equals("false")) // If the above line is "false" then this will read another line...if this line is "true" then you completely skip the if/else { fill[count] = false; }Please do not ask for code as refusal often offends.
- 08-06-2012, 11:30 AM #8
- 08-06-2012, 03:26 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Re: Help with loading
Oh, wow. Thanks a lot for the help. i really appreciate it. I'll go do that right away.
- 08-06-2012, 03:28 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Loading a CSV into an array.
By OllyHal in forum New To JavaReplies: 1Last Post: 02-09-2012, 02:02 AM -
Loading Servlet
By Mindhunter74 in forum Java ServletReplies: 1Last Post: 10-05-2009, 10:11 PM -
loading JInternalFrames
By vishakha in forum AWT / SwingReplies: 5Last Post: 07-23-2008, 03:58 PM -
loading a new Jframe
By Ebylord in forum New To JavaReplies: 0Last Post: 07-22-2008, 08:31 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks