javajava.util.scanneruser-inputjava-io

java: input.next no longer accepting user input


i fixed most of the new issues but now im getting an empty character literal and unclosed character literal error on line: char a = input.next('');

Below is my new code but im not entirely sure how to fix this error.

import java.util.*;
import java.io.BufferedReader;
public class JesseSabatinihw {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char lottoGameSelection; // user inputs whether or not they want to play the Lottery Game
        int game; // user input of game 3, 4, or 5 as integer
        int times; // user input of times game is played as integer
        int randNum; // random number generator creates an integer
        int lotNum; //lottery number generated as an integer
        int sum = 0; // initializing the sum of all integers as 0 to start with

        System.out.println("Do you wish to make lottery game selections?");
        char a = input.next('');
        if(a == 'Y' || a =='y') {       
            System.out.println("Which lottery game do you want to play!");
            System.out.println("Enter 3 for Pick 3");
            System.out.println("Enter 4 for Pick 4");
            System.out.println("Enter 5 for Pick 5");
            game = input.nextInt();

            System.out.print("How many games would you like to play? ");
            times = input.nextInt();
            System.out.println("Thank you! The numbers selected were: ");
            // generates random lottery number specific to the chosen game 3, 4, or 5 numbers per games specified by user input
            Set<Integer> numbers = new HashSet<>();
            for(int i = 0; i < times; i++) {
                lotNum = 0;
                for(int j = 0; j < game; j++) {
                    do {
                        randNum = (new java.util.Random()).nextInt(10); // calls on the java.util.Random class to randomly select number for game
                    // prevents the generation of duplicate numbers per game
                    } while (numbers.contains(randNum));
                    numbers.add(randNum);
                    lotNum = (lotNum * 10) + randNum; // selects random integers 0 - 9
                    System.out.print(randNum); // prints randomly generated number
                    sum += randNum; // adds all random numbers generated
                }
                numbers.clear(); // refreshes the number generator preventing duplication of numbers and preventing the code to stop if all int 0-9 are used previously
                System.out.println(); // prints each game on it's own line
            }
            System.out.println("Sum of the numbers of all games: " + sum); //prints the total of all randomly generated lottery numbers
        } else {
            if (a == 'N' || a == 'n'); {
                System.exit(0);
            }
        }
    }
}

Solution

  • You can use a Set to store numbers you already got and then redraw until you get a number that is not in the Set.

    System.out.println("Thank you! The numbers selected were: ");
    Set<Integer> numbers = new HashSet<>();
    for(int i = 0; i < times; i++) {
      lotNum = 0;
      for(int j = 0; j < game; j++) {
        do {
          randNum = (new java.util.Random()).nextInt(10);
        } while (numbers.contains(randNum));
        numbers.add(randNum);
        lotNum = (lotNum * 10) + randNum;
        System.out.print(randNum);
        sum += randNum;
      }
      numbers.clear();
      System.out.println();
    }
    

    I am not sure in what scope you want the numbers to be unique so maybe you have to pull the creation of the set inside the outer loop - or just call number.clear() when you want to reset it.