#next() problem
20 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @hot comet! Please use
/closeor theClose Postbutton 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.
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
I mean, the simpler approach is nextInt() followed by next(). What did they expect with nextLine()?
I get an noSuchelementexception when I run it with that
With your input that shouldn't be the case. Show the code
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
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;
}
}
Ah, it was supposed to be hasNext(), not hasNextLine()
oh
alright it kinda worked? it printed out the last integer but im trying to print out all the integers in the text
Then rather than assign the number, print it
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.