#next() problem

20 messages · Page 1 of 1 (latest)

hot comet
#

I've been trying to figure out how to repeat this loop of printingvout the integers and skipping the string with a .next Line but I can get it to repeat even though it's in a while loop?

vital flareBOT
#

This post has been reserved for your question.

Hey @hot comet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

agile marsh
#

You could use a try catch where you try to use nextInt() and catch an InputMismatchException. In the catch you can simply 'move on' using scanner.next()

#

And you obviously need to increment your num

lunar grove
#

I mean, the simpler approach is nextInt() followed by next(). What did they expect with nextLine()?

hot comet
lunar grove
#

With your input that shouldn't be the case. Show the code

lunar grove
#

You need to stop pics and paste code

#

Same for the input

#

We can't try it otherwise, as you should not have needed to be told

hot comet
#

import java.util.;
import java.io.
;
public class Mc {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("money.txt"));
System.out.println(countMoney(input));
}
public static int countMoney(Scanner input){
int num = 0;
while(input.hasNextLine()){
num = input.nextInt();
input.next();
}
return num;
}
}

lunar grove
#

Ah, it was supposed to be hasNext(), not hasNextLine()

hot comet
#

oh

#

alright it kinda worked? it printed out the last integer but im trying to print out all the integers in the text

lunar grove
#

Then rather than assign the number, print it

hot comet
#

Thank you

vital flareBOT
# hot comet Thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.