d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Shupaid File Io..
Add Reply New Topic New Poll
Member
Posts: 9,920
Joined: Apr 2 2008
Gold: 0.00
Oct 27 2012 08:12pm
No syntax error, compiles nice and fine.

I prompt the user for an input, the input becomes my file, the scanner reads the file. Okay, great.

While (readFromFile.hasNext() )
{
x = readFromFile.nextLine();

y = readFromFile.nextInt();

z = readFromFile.nextDouble();
}


all initialized, everything else perfect. BUT. i keep getting this error when i run it saying

Java,util.NoSuchElementException: null (in java.util.scanner)

What do..

This post was edited by Foloed on Oct 27 2012 08:21pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 08:37pm
show me your text file. im not sure why you're using all three nextLine, nextInt, and nextDouble.

you're assuming the file is well formatted. i'm guessing either it's NOT well formatted or your code is assuming a different format than what it's really in

This post was edited by carteblanche on Oct 27 2012 08:48pm
Member
Posts: 9,920
Joined: Apr 2 2008
Gold: 0.00
Oct 27 2012 08:57pm
its formatted as a letter on one line, number on the next, and a number on the next.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 09:27pm
Quote (Foloed @ Oct 27 2012 10:57pm)
its formatted as a letter on one line, number on the next, and a number on the next.


add a println or debug every time you read something. tell me which one (nextInt, nextLine, nextDouble) fails, what it reads successfully, and what's left.
Member
Posts: 9,920
Joined: Apr 2 2008
Gold: 0.00
Oct 27 2012 09:34pm
Its the nextInt that fails :(
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 09:59pm
Quote (Foloed @ Oct 27 2012 11:34pm)
Its the nextInt that fails :(


so...try stuff out? try changing it to nextLine() again and println or debug to find out what it returns.
Member
Posts: 9,920
Joined: Apr 2 2008
Gold: 0.00
Oct 27 2012 10:10pm
Okay, i have isolated my problem.. Reading the file incorrectly.

filePathInput = keyboard.next();
File lol = new File (filePathInput);
scanner readFromFile = new Scanner (lol);

Not sure how to fix. I know that it is the stupid inputted file path input though. Do i need to add quotations to file path input?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 10:16pm
Quote (Foloed @ Oct 28 2012 12:10am)
Okay, i have isolated my problem.. Reading the file incorrectly.

filePathInput = keyboard.next();
File lol = new File (filePathInput);
scanner readFromFile = new Scanner (lol);

Not sure how to fix. I know that it is the stupid inputted file path input though. Do i need to add quotations to file path input?

add some friggin logging dude
Code
filePathInput = keyboard.next();
log.debug("filePathInput=" + filePathInput);

File lol = new File (filePathInput);
log.debug("file exists: " + lol.exists());

scanner readFromFile = new Scanner (lol);
log.debug("about to start WHILE");

while (readFromFile.hasNext() )
{
log.debug("WHILE started");

x = readFromFile.nextLine();
log.debug("x=" + x);

y = readFromFile.nextInt();
log.debug("y=" + y);

z = readFromFile.nextDouble();
log.debug("z=" + z);
}
log.debug("loop done");


then show me your log file. this is something you should be doing on your own. instead you need to post 4+ times to get all the information.

i'm guessing there's a space when the user types the file path, and keyboard.next() cuts it off

This post was edited by carteblanche on Oct 27 2012 10:17pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll