Search code examples
javaobjectjava.util.scannerdo-loops

The program needs to keep looping till the key "Q"/"q" is typed


The program extracts the digit and I want it to keep looping till the key "Q" / "q" is typed by the user. For example when the user hits the "O" key, the program should print the ones digit of the number they inputted and so on for any 3-digit number the user inputs. When I run the code right now, there is no output but there is also no error.

import java.util.Scanner;
public class DigitExtractor {

    public static void main(String[] args) 
            throws java.io.IOException{

        char input;
        input = (char) System.in.read();
        Scanner s = new Scanner(System.in);

        variables Num = new variables();

        do {

            System.out.print("Enter an integer: ");
            String wholeNumber = s.nextLine();

            Num.ones = wholeNumber.charAt(2);
            Num.tens = wholeNumber.charAt(1);
            Num.hundred = wholeNumber.charAt(0);

            System.out.println("show (W)hole number.");
            System.out.println("show (O)nes place number.");
            System.out.println("show (T)ens place number.");
            System.out.println("show (H)undreds place number.");

            input = (char) System.in.read();
            System.out.println("Enter your choice: " + input);

            if(input == 'W' || input == 'w') {
                System.out.println(Num.WholeNum);
            }
            else if(input == 'O' || input == 'o') {
                System.out.println(Num.ones);
            }
            else if(input == 'T' || input == 't') {
                System.out.println(Num.tens);
            }
            else if(input == 'H' || input == 'H') {
                System.out.println(Num.hundred);

            }
        } while (input == 'q');
    }
}

class variables {
    char hundred; 
    char tens; 
    char ones;
    char WholeNum;
}

Solution

  • The reading was getting muddled. To read an integer with a scanner, I chose nextInt instead. That helped. I went with your approach of not breaking things down into smaller chunks. And (revision) I use only the scanner to do reading, even of the character for choice. I also put the prompt before you had to press the option so you would know.

           public static void main(String[] args)
    
    
     throws java.io.IOException {
           int hundred; //my compiler was fussy about having the extra class
           int tens;
           int ones;
           char input = ' '; //initialize outside loop
           Scanner s = new Scanner(System.in);
    
    
    
       do {
    
           System.out.print("Enter an integer: ");
           int wholeNumber = s.nextInt();
    
           ones = wholeNumber % 10;
           tens = (wholeNumber / 10) % 10;
           hundred = (wholeNumber / 100) % 10;
    
           System.out.println("show (W)hole number.");
           System.out.println("show (O)nes place number.");
           System.out.println("show (T)ens place number.");
           System.out.println("show (H)undreds place number.");
           System.out.println("(Q)uit");
           System.out.print("Enter your choice: ");
           input = s.next().trim().charAt(0); //using scanner only
           //System.out.println("Enter your choice: " + input);
    
           if (input == 'W' || input == 'w') {
               System.out.println(wholeNumber);
           } else if (input == 'O' || input == 'o') {
               System.out.println(ones);
           } else if (input == 'T' || input == 't') {
               System.out.println(tens);
           } else if (input == 'H' || input == 'H') {
               System.out.println(hundred);
    
           }
       } while ((input != 'q') && (input != 'Q'));
    
    
    }
    
       }