1

I'm trying to get the users input to output on the next line to look something like this.

_ _ _ _ _ a

Note** They'll be picking their string length before this but I'm not too worried about that right now. Just want to figure out how to get their input to output.

Here is my code so far:

    while (!userCorrect) {
        Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
            String letter = input.nextLine();
            for (int i=0; i<letter.length(); i++){ //this is where I'm getting lost
                System.out.println("_" + letter.length());
            }
            if (letter.length () > 1) { //code will print an error if the user selects more than 1 character
            System.out.println("You should not enter more than 1 character");
                }
            else {
            System.out.println("end (round " + roundNumber + ")");
            roundNumber++; 
            }

I created a for loop so it can print the lines and the character depending on their input. It's not working and it's giving the response like:

Please enter your desired string length: 5 Start guessing the string: Guessing (round 1): Choosing your letter from a-z: a _1 end (round 1) Guessing (round 2): Choosing your letter from a-z:

Any ideas or hints on how I can tweak this to make the output show?

Thanks in advance.

Gaetano
  • 29
  • 5
  • check this question: https://stackoverflow.com/questions/1001335/java-gotoxyx-y-for-console-applications basically you have to read the input without showing it and manually move the cursor. There are libraries for doing that. – BigMike Sep 26 '18 at 15:21
  • Please show me more details of the desired output like. I cant guess what you want.. – KYHSGeekCode Sep 26 '18 at 16:11
  • Are you building a hangman game? – KYHSGeekCode Sep 26 '18 at 16:12
  • no no, i'm trying to build a guessing game that includes random characters and the user will need to guess the string – Gaetano Sep 26 '18 at 18:41

1 Answers1

1

Try this.

while (!userCorrect) {
        //Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
        char letter=0;
        try
        {
             letter=System.in.read();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //String letter = input.nextLine();
        for (int i=0; i < your_answer_string_length; i++)
        {
            //this is where I'm getting lost
            System.out.print("_");
        }
        System.out.println(letter);
        System.out.println("end (round " + roundNumber + ")");
        roundNumber++; 
    }

Is this what you want?

KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30
  • Hi,Thanks for your input. It outputs the users response but I want it to not output it if they choose more than 1 character and I want it to output the error I have. Do you think if I put the for loop within the if statement, that will work? – Gaetano Sep 26 '18 at 15:46
  • @Gaetano you don't have to show any error message because systrm.in.read() will automatically block the user from entering more than one. – KYHSGeekCode Sep 26 '18 at 16:05
  • Thanks! I think i got what I need to figure this out. Appreciate your help! – Gaetano Sep 26 '18 at 16:15