Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Loop/User Input from Scanner

Tags:

java

Making just a simple basketball program where I ask for the home team name, how many games are in the season, and then in a loop ask for the next team game. Basically when I start the do-while loop, it works great, unless the user types in for example, "Ohio State." The out put will then go from "6 games remaining" to "4 games remaining" for example. Usually it will just ask opponent?, then decrement by one game.

How can I fix so that a 2 word basketball team name doesn't decrement twice?

import java.util.Scanner;

public class Basketball2 {


public static void main(String[] args) throws java.io.IOException {
    Scanner scanInput  = new Scanner(System.in);
    String sHomeTeam;
    String sAwayTeam;
    int iNumGames;
    int iGamesLeft = 0;

    System.out.println("Enter home team's name: ");
    sHomeTeam = scanInput.nextLine();
    System.out.println(sHomeTeam);

    System.out.println("How many games are in the home team's basketball season?");
    iNumGames = scanInput.nextInt();
    System.out.println(iNumGames);

    //start looping
    do {
        System.out.println("Enter opponent team's name: ");
        sAwayTeam = scanInput.next();
        System.out.println(sAwayTeam);
        iGamesLeft = --iNumGames;
        System.out.println("There are " + iGamesLeft + " games left in the basketball season");


    }//end do
    while(iGamesLeft > 0);
like image 387
K2SO Ghost Spirit Avatar asked Feb 06 '13 02:02

K2SO Ghost Spirit


People also ask

How do you input a loop in Java?

To take input of an array, we must ask the user about the length of the array. After that, we use a Java for loop to take the input from the user and the same for loop is also used for retrieving the elements from the array.

How do I get an int from a Scanner?

To read integers from console, use Scanner class. Scanner myInput = new Scanner( System.in ); Allow a use to add an integer using the nextInt() method.

How do you accept a character from a Scanner in Java?

To take a char input using Scanner and next(), you can use these two lines of code. Scanner input = new Scanner (system.in); char a = input. next().


2 Answers

Replace: sAwayTeam = scanInput.next(); with sAwayTeam = scanInput.nextLine(); The reason it loops twice is because scanInput.next(); only returns one token (e.g. word) at a time. When you enter two words it doesn't need receive more input from the user before continuing a second time because it already has another word to return. Hence the double loop.

You also need to take care of the line of code that calls nextInt(). This works like the next() method, but, instead of a token (word), it scans in just one character as an int. Try this: after iNumGames = scanInput.nextInt(); put scanInput.nextLine(); This should clear scanInput of anything that is making it skip. Note: because of the way that your code is written, this will only read one character. If you need to read more than one character you should use nextLine() and assign its value to an integer.

like image 95
Don Avatar answered Sep 23 '22 14:09

Don


Whatever is said in the answer given by Donny Schrimsher is correct. All that you have to do now is after getting the number of games in the home team's basketball season i.e.

System.out.println("How many games are in the home team's basketball season?");
iNumGames = scanInput.nextInt();

You have to add

scanInput.nextLine();

This is because after entering the number of games you press enter key (end of line) and nextInt() method takes the number of games and not the end-of-line. This end-of-line is consumed by the nextLine() method which Donny Schrimsher mentioned in the do-while loop. SO to avoid this you add an extra nextLine() method.

Thus it has to be

System.out.println("How many games are in the home team's basketball season?");
iNumGames = scanInput.nextInt();
scanInput.nextLine();
System.out.println(iNumGames);

plus the changes mentioned by Donny Schrimsher.

Thanks

like image 28
Rameshwar.S.Soni Avatar answered Sep 25 '22 14:09

Rameshwar.S.Soni