#why doesnt java run the lines in the for loop more than once?

46 messages · Page 1 of 1 (latest)

shadow wraith
#

grabbing code

trail socketBOT
#

This post has been reserved for your question.

Hey @shadow wraith! 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.

shadow wraith
#
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]
trail socketBOT
cyan zodiac
#

try stepping through it with a debugger

shadow wraith
#

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

cyan zodiac
#

what happens if you both have a println and the input?

shadow wraith
#

let me check

cyan zodiac
#

And does the program end or is it just stuck?

#

And what input do you enter?

#

or is the input the thing with >?

shadow wraith
shadow wraith
shadow wraith
#

it seems to work then

cyan zodiac
#

what's the value of favTeam and gamesPlayed? Use a debugger to find out, not println statements

cyan zodiac
shadow wraith
#

fav team for testing is always 1, and gamesPlayed is always 4

cyan zodiac
#

Did you check that with a debugger?

shadow wraith
#

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
cyan zodiac
#

I thought o would be 1 at the beginning..?

shadow wraith
#

it shouldnt really matter

#

its just taking input for the number of games played

shadow wraith
#

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

cyan zodiac
#

Do you have multiple Scanners in your application?

shadow wraith
#

no

shadow wraith
#

main just calls the method

cyan zodiac
#

Can you reproduce the problem with the minimal tedt code above?

shadow wraith
#

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

trail socketBOT
cyan zodiac
#

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)

trail socketBOT
#

💤 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.