#why doesnt java run the lines in the for loop more than once?
46 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @shadow wraith! 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.
import java.util.Scanner;
public class ChanceOfWinning {
Scanner input = new Scanner(System.in);
public int calculateChance() {
int favTeam = Integer.parseInt(input.nextLine());
int gamesPlayed = Integer.parseInt(input.nextLine());
for (int i = 0; i <= gamesPlayed; i++) {
String data = input.nextLine();
}
return 0;
}
}
> = i entered this
output
> 1
> 4
[end of program]
try stepping through it with a debugger
no errors are returned, it just doesnt run the above for loop
it works without the input in there, as in this works
for (int i = 0; i <= gamesPlayed; i++) {
System.out.println(i);
output:
0
1
2
3
what happens if you both have a println and the input?
let me check
And does the program end or is it just stuck?
And what input do you enter?
or is the input the thing with >?
it ends
yeah any time i put > i mean to say thats me entering something
it seems to work then
what's the value of favTeam and gamesPlayed? Use a debugger to find out, not println statements
using input and not println (where it doesn't work)
fav team for testing is always 1, and gamesPlayed is always 4
Did you check that with a debugger?
there set in that input and never changed again
they're*
built a small test copy, with bad var names, etc
import java.util.Scanner;
public class Cow {
public void cow() {
Scanner i = new Scanner(System.in);
int g = Integer.parseInt(i.nextLine());
for (int o = 0; o <= g; o++) {
System.out.println(o);
String r = i.nextLine();
}
}
}
favTeam is never actually used (yet) in the actual version so i didnt bother adding it here
> 4
0
>
1
>
2
>
3
>
4
I thought o would be 1 at the beginning..?
nvm
for a little more context, its going to calculate the odds of a specific team winning based off their scores
the data in the for loop will be
team1 team2 team1Score team2Score
1 2 5 3 for example, saying that team one beat team 2 5 to 3
Do you have multiple Scanners in your application?
no
litterally the entire thing outside of Main
main just calls the method
Can you reproduce the problem with the minimal tedt code above?
now i have the opposite problem?
>4
>
>
>
>
>
>
>
[end of program]
java is not on my side today
thanks for the help, imma go sleep on it because its 1 am
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.
ckeck with a debugger
ir maybe run the program from the command line - maybe your IDE is causing these issues
(println statements are not a debugger)
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.