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?
No description
No description
14 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @stapps! 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.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan2y ago
I mean, the simpler approach is nextInt() followed by next(). What did they expect with nextLine()?
stapps
stappsOP2y ago
I get an noSuchelementexception when I run it with that
Kyo-chan
Kyo-chan2y ago
With your input that shouldn't be the case. Show the code
stapps
stappsOP2y ago
No description
Kyo-chan
Kyo-chan2y ago
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
stapps
stappsOP2y ago
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; } }
Kyo-chan
Kyo-chan2y ago
Ah, it was supposed to be hasNext(), not hasNextLine()
stapps
stappsOP2y ago
oh alright it kinda worked? it printed out the last integer but im trying to print out all the integers in the text
Kyo-chan
Kyo-chan2y ago
Then rather than assign the number, print it
stapps
stappsOP2y ago
It worked Thank you
JavaBot
JavaBot2y ago
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.
JavaBot
JavaBot2y ago
Post Closed
This post has been closed by <@467513671971766272>.
Want results from more Discord servers?
Add your server