1

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;
}
  • 2
    `while (input != 'q');` I think is what you want. – markspace Jul 17 '19 at 02:54
  • I've tried that, there is also no output. @markspace – superfersur Jul 17 '19 at 02:55
  • I would suggest breaking this down into smaller bits so you can isolate what is getting read when. I'd probably also recommend using parseInt, % and / in case things are not the length you expect. But write a getChoice and a getNumber method. – Jeremy Kahan Jul 17 '19 at 03:11
  • Using a method will not really help me, I just need to see why the code is not outputting anything. @JeremyKahan – superfersur Jul 17 '19 at 03:16
  • What is the first `System.in.read()` meant to accomplish? The program will sit waiting for user to press some key, without having given the user any clue that a key needs to be pressed. Until the user presses something, the program will never advance to the "Enter an integer" prompt. – Kevin Anderson Jul 17 '19 at 03:18
  • It is meant to read the input of the user as I am only using one scanner, say if I were to delete it that line, there is an error in the while loop that states "local variable input may not have been initialized". How would I fix this? @KevinAnderson – superfersur Jul 17 '19 at 03:21
  • 1
    You could say char input='W' when you declare it. But mixing the scanner and the reading of a character may also be throwing things off. See https://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner for you to read the character from the scanner. – Jeremy Kahan Jul 17 '19 at 03:27
  • `System.in` is **line-based**, i.e. it only receives full lines of text from the console. Java does not see individual keystrokes. Also, mixing `s.nextLine()` and `System.in.read()` is a bad idea. – Andreas Jul 17 '19 at 03:44
  • WholeNum should not be a char if it is supposed to hold 3 characters. – Jeremy Kahan Jul 17 '19 at 03:47
  • How would I fix that @Andreas – superfersur Jul 17 '19 at 03:47
  • Print `Enter your choice:` then call `s.nextLine()` to get the choice. Don't call `System.in.read()`. – Andreas Jul 17 '19 at 03:49

1 Answers1

1

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'));


}

   }
Jeremy Kahan
  • 3,129
  • 1
  • 6
  • 19
  • This also does not work because it won't let my enter a key @Jeremy Kahan – superfersur Jul 17 '19 at 04:04
  • I just tested in bluej, but let me think on it to make sure it can cross ide's – Jeremy Kahan Jul 17 '19 at 04:06
  • revised so it uses scanner only. That should now let you enter a key and tell you when it is expecting to. Let me know if that helps, please. – Jeremy Kahan Jul 17 '19 at 04:21
  • Yes it works, but I have to hit enter twice for it to display the next output, why is that? @Jeremy Kahan – superfersur Jul 17 '19 at 04:24
  • strange. I do not. If I say T, for instance, and hit enter, out pops the tens digit. Saying nextLine() gave me an error because it got an empty string after trimming, which made charAt(0) fail. Maybe println the "Enter your choice: You are hitting enter after the integer, I assume. – Jeremy Kahan Jul 17 '19 at 04:32
  • Thank you so much for helping @Jeremy Kahan – superfersur Jul 17 '19 at 04:38
  • sure thing. You might want to add code to guard against if the person just hits a space or return (or both) when they're supposed to choose, that you not do charAt(0) on what turns out to be an empty string. I wish I had a better idea for you on the double enter. – Jeremy Kahan Jul 17 '19 at 04:43
  • My bedtime, sorry – Jeremy Kahan Jul 17 '19 at 04:45