next() problem
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?
14 Replies
⌛
This post has been reserved for your question.
Hey @stapps! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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
It worked
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.
Post Closed
This post has been closed by <@467513671971766272>.