Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: Help with loading

  1. #1
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default 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:
    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;
    	}
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with loading

    How are you running that code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default 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.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Help with loading

    Quote Originally Posted by DarrylBurke View Post
    How are you running that code?
    Quote Originally Posted by waker3210 View Post
    What do you mean?
    I think he's asking if you are using an IDE or just a text editor and command prompt.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Help with loading

    Oh, well i'm using an IDE, JCreator to be more exact.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Help with loading

    You're reading the line twice if nextLine() returns "false".

    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;
    }
    So store the String returned by nextLine() into a variable then test the variable. Don't read and test in one go.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with loading

    Quote Originally Posted by Tolls View Post
    You're reading the line twice if nextLine() returns "false".
    D'oh. I should have spotted that!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Re: Help with loading

    Oh, wow. Thanks a lot for the help. i really appreciate it. I'll go do that right away.

  10. #10
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Loading a CSV into an array.
    By OllyHal in forum New To Java
    Replies: 1
    Last Post: 02-09-2012, 02:02 AM
  2. Loading Servlet
    By Mindhunter74 in forum Java Servlet
    Replies: 1
    Last Post: 10-05-2009, 10:11 PM
  3. loading JInternalFrames
    By vishakha in forum AWT / Swing
    Replies: 5
    Last Post: 07-23-2008, 03:58 PM
  4. loading a new Jframe
    By Ebylord in forum New To Java
    Replies: 0
    Last Post: 07-22-2008, 08:31 PM

Posting Permissions

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