0

I am studying Java now and I have a problem. In my scenario I created a series of methods and I asked the user to choose from one of them. After choosing the desired method there will be a message that will ask to input a string or an integer. My problem is After choosing the desired method it skipped the second phase which is asking to input any string/integer. Hope you'd understand my problem.

Here's my simple code:

import java.util.*;

import java.io.*;

public class MachineProblemChoser {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        int chosenProblem;

        System.out.println("Choose a problem you want to process");
        System.out.println("***********************************************");
        System.out.println("  1 -- Sum of digit");
        System.out.println("  2 -- Days of the month");
        System.out.println("  3 -- Largest number");
        System.out.println("  4 -- Fibonacci");
        System.out.println("  5 -- Palindrome");
        System.out.println(" -1 -- Exit");
        System.out.println("***********************************************");
        System.out.print(" Choose from(1-5) and -1 for exit: ");
        chosenProblem = console.nextInt();

        String numberSeries;
        int inputs;

        switch(chosenProblem) {

            case 1:

                //FROM THIS PART IT SKIPPED THE INPUT FROM THE USER
                System.out.println("SUM OF DIGIT");
                System.out.println("Input any integer separated by space:");
                numberSeries = console.nextLine();

                System.out.println("Count is: " + numberSeries.length());

            break;

            case 2:
                //machineProblemTwo();
            break;

            case 3:
                //machineProblemThree();
            break;

            case 4:
               // machineProblemFourth();
            break;

            case 5:
               //machineProblemFifth();
            break;

            case -1:
               exitProgram();
            break;

            default: 
               System.out.println(" Oops: Choose a valid number for the selection!");

        }

    }


    public static void machineProblemOne(String chosenNumber) {
        System.out.println("Chose number are: " + chosenNumber);  
    }

    public static int validateInput(int inputs) {

        return 0;
    }

    public static void exitProgram() {
        System.out.println(" Thanks for using the Machine Problem Choser! ");
    }

}

Here's the output:

Choose a problem you want to process
***********************************************
  1 -- Sum of digit
  2 -- Days of the month
  3 -- Largest number
  4 -- Fibonacci
  5 -- Palindrome
 -1 -- Exit
***********************************************
 Choose from(1-5) and -1 for exit: 1
SUM OF DIGIT
Input any integer separated by space:         ---- From this it skipped
Count is: 0
Jerielle
  • 5,994
  • 27
  • 75
  • 146

2 Answers2

1

okay I tested out your problem and this might work:

Try replacing this:

numberSeries = console.nextLine();

with this:

numberSeries = System.console().readLine();

It might help.

CSCH
  • 521
  • 5
  • 14
1

scanner.nextInt(); reads only the next integer from input. You need to clear the remaining carriage return. Try this:

...
System.out.println("***********************************************");
System.out.print(" Choose from(1-5) and -1 for exit: ");
chosenProblem = console.nextInt();
console.nextLine();//this clears out the rest of the input line

String numberSeries;
int inputs;
....
Quicksilver002
  • 725
  • 5
  • 12
  • Ok I got it so everytime I performed a user input I always need to clear the remaining carriage? – Jerielle Mar 28 '15 at 03:30
  • Every time you read a single int, character, etc, be aware that there's more in the input buffer. If you want to ensure that the buffer is clear, do a nextLine(). – Quicksilver002 Mar 28 '15 at 03:56