scanner doesnt seem to work if number of lines aren't even..why?

im making a simple program (fooling around with java) and i tried uploading a file that have an uneven amount of lines. There's a error at the end of the program.. I post code + my txt file..any comments would be appreciated..

import javax.swing.*;

import java.util.*;

import java.io.*;

publicclass TestOnThursday{

publicstaticvoid main (String [] args)throws IOException{

JFileChooser jfc =new JFileChooser();

jfc.showOpenDialog(null);// opens the chooser

String file = jfc.getSelectedFile().getAbsolutePath();

Scanner sc =new Scanner(new File(file));

String eggs = sc.nextLine();

System.out.println(eggs);

while (sc.hasNext()){

String cheese = sc.nextLine();

String ham = sc.nextLine();

System.out.println(cheese);

System.out.println(ham);

}

System.out.println("end of file");

}

}

txt file(dont ask why I put such randomness..i have a test in about 2 days and i need to get ansome grade)

handsinmypockethandsinmypockethandsinmypocket

iam

thesmartest

kid

in

the

world

yay!!!!!!

[1846 byte] By [moonmastera] at [2007-11-26 12:21:39]
# 1
It's because you call hasNext() once, but then call nextLine() twice. Scanner itself is fine.
paulcwa at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 2

Because each time the loop iterates, youre printing out the next two items in the file.

while (sc.hasNext()) {

String cheese = sc.nextLine();

String ham = sc.nextLine();

System.out.println(cheese);

System.out.println(ham);

}

I think you want something like this:

while (sc.hasNext()) {

String line = sc.nextLine();

System.out.println(line);

}

And please use meaningful variable names.

CaptainMorgan08a at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 3
okay...still confused..im just curious what's difference if i put sc.next and sc.nextLine..can u explain a bit more..please be gentle..im not used to complicating computer language..
moonmastera at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 4
+ wats wrong with putting sc.nextLine twice? shouldnt my while loop of sc.hasNext stop this?
moonmastera at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 5
> + wats wrong with putting sc.nextLine twice? shouldnt> my while loop of sc.hasNext stop this?You are checking if there is one line after your current spot. You are then reading in the next two lines.
CaptainMorgan08a at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 6
oh okay morgan..i get it..but lets say i want them stored in two different arrays..would my program work? + can u link me to a sc.next sc.nextLine tutoral page..if you have one..Message was edited by: moonmaster
moonmastera at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 7
It throws an error because when you do the while loop, it only checks for one line, it does not check if the line after it exists. I suggest you only read one line at a time and output it.-Yihuan
Bz_Unknowna at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...
# 8
ah i understand the problem..if i dont put an even amount of lines for each of my outputs. sc.nextline for eggs and sc.nextLine for ham it will mess up..
moonmastera at 2007-7-7 15:13:45 > top of Java-index,Archived Forums,Socket Programming...