1

basically what I am trying to do is:

Get the user to enter a 5 digit number. If all of the digits in the number are Odd, it will print out "All of the digits are odd" and if all of the digits are not Odd, then it will print out "Not all of the digits are odd", and then it will ask the user if they want to Enter a new Number by pressing Y or N and then it will ask them for another 5 digit number and repeat.

Here is my problem:

When a user does not enter a 5 digit number, E.G enters a string/character, then I want it to say "That was not a number, please try again", I tried to do this with try and catch, I can make it so it says try again, but I am unsure on how to make the code return back to the beginning after an error has been thrown. Sorry I am bad at java :-/ I also have this problem when user does not enter Y or N and enter a number.

code:

package veryoddnumber;

import java.util.*;

public class VeryOddNumber {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a 5 digit number...");
        boolean yesno;
        do {

            try {
                int number = scan.nextInt();
                int length = String.valueOf(number).length();

                if (length == 5) {
                    String digits = String.valueOf(number);
                    char number1 = digits.charAt(0);
                    char number2 = digits.charAt(1);
                    char number3 = digits.charAt(2);
                    char number4 = digits.charAt(3);
                    char number5 = digits.charAt(4);

                    if (number1 % 2 != 0 && number2 % 2 != 0 && number3 % 2 != 0 && number4 % 2 != 0 && number5 % 2 != 0) {
                        System.out.println("All of the numbers are odd...");
                    } else {
                        System.out.println("Not all of the numbers are odd...");
                    }

                    System.out.println("Would you like to enter another number? (Y/N)");

                } else {
                    System.out.println("You did not enter a 5 digit number! Try again...");
                    System.out.println("Enter a 5 digit number...");
                }
            } catch (Exception e) {
                System.out.println("That was not a number! Try again...");

            }
            try {

                char letter = scan.next().charAt(0);
                if (letter == 'Y' || letter == 'y') {
                    yesno = true;
                } else if (letter == 'N' || letter == 'n') {
                    break;
                } else {
                    System.out.println("I will take that as a no!");
                    break;
                }
                System.out.println("Enter a 5 digit number...");
            } catch (Exception e) {
                System.out.println("You did not enter a letter...");

            }
        } while (yesno = true);
        System.out.println("Goodbye!");
    }

}
  • 1
    your are abusing a cast here. `(number1 % 2)` is using the ascii code of number1, not its value. For example if `number1` is `"1"`, `(number1 % 2)` is `(49 % 2)` not `(1 % 2)`. But you are lucky, `0` has ascii code 48. –  Nov 28 '14 at 18:10
  • What does that mean? :/ – Louie Morris Nov 28 '14 at 18:10
  • Ok, thanks, but could you try to answer my question please – Louie Morris Nov 28 '14 at 18:14
  • Wouldn't it be easier to check if one digit is not odd instead of if all the digits are odd? – TNT Nov 28 '14 at 18:15
  • Check out the code in this other thread: http://stackoverflow.com/questions/19129509/how-can-i-count-the-number-of-odd-evens-and-zeros-in-a-multiple-digit-number-i – Ensar Hatipoglu Nov 28 '14 at 18:16
  • Instead of getting individual char from String, get the digit itself, see [this](http://stackoverflow.com/questions/2051817/return-first-digit-of-an-integer). It speaks about first char but you can use it for others too – mprabhat Nov 28 '14 at 18:16

1 Answers1

2

I've just rearranged some of your logic, and added a couple continue statements. You were very close, you just needed to rearrange some things.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    
    boolean yesno = true;
    while (yens == true) {
        System.out.println("Enter a 5 digit number...");
        try {
            int number = scan.nextInt();
            int length = String.valueOf(number).length();

            if (length == 5) {
                String digits = String.valueOf(number);
                char number1 = digits.charAt(0);
                char number2 = digits.charAt(1);
                char number3 = digits.charAt(2);
                char number4 = digits.charAt(3);
                char number5 = digits.charAt(4);

                if (number1 % 2 != 0 && number2 % 2 != 0 && number3 % 2 != 0 && number4 % 2 != 0 && number5 % 2 != 0) {
                    System.out.println("All of the numbers are odd...");
                } else {
                    System.out.println("Not all of the numbers are odd...");
                }
            } else {
                System.out.println("You did not enter a 5 digit number! Try again...");
                continue;
            }
        } catch (Exception e) {
            System.out.println("That was not a number! Try again...");
            //A newline might still be stuck in the scanner, so clear it
            scan.nextLine();
            continue;
        }
        try {
            System.out.println("Would you like to enter another number? (Y/N)");

            char letter = scan.next().charAt(0);
            if (letter == 'Y' || letter == 'y') {
                continue;
            } else if (letter == 'N' || letter == 'n') {
                yesno = false;
            } else {
                System.out.println("I will take that as a no!");
                yesno = false;
            }
        } catch (Exception e) {
            System.out.println("You did not enter a letter...");
            scan.nextLine();
        }
    }
    System.out.println("Goodbye!");
}

By the way, you have a lot of other issues in the code that should be addressed, but I simply answered the question that you asked, making the execution return the to top of the loop after an exception is thrown.

miken32
  • 42,008
  • 16
  • 111
  • 154
David.Jones
  • 1,413
  • 8
  • 16
  • 1
    Hi, thanks, when I have the code you put, when an exception is thrown on the first part, when I run it is just keeps saying: Enter a 5 digit number... That was not a number! Try again... Enter a 5 digit number... That was not a number! Try again... Enter a 5 digit number... That was not a number! Try again... – Louie Morris Nov 28 '14 at 18:24
  • I've edited my code to fix this problem. I didn't realize a newline value might be stuck in the scanner. See my updated code above. – David.Jones Nov 28 '14 at 18:28
  • No problem! Do you understand the code? I don't want to simply solve your problem and you not understand why your code was wrong. If you would like an explanation please let me know, or feel free to ask questions... – David.Jones Nov 28 '14 at 18:36
  • 1
    Yes, I do understand it ;), what were the problems with the code that you didn't put into the answer? – Louie Morris Nov 28 '14 at 18:39
  • One example, when you get to "Would you like to enter another number? (Y/N)", you only strip the first character from the response. Enter "Y \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n". This should be handled by clearing the input scanner. Also, as some stated in the comments above, your (number % 2) logic isn't actually doing what you want. Its taking the ASCII value of the number (string "1" is 49 in ASCII), so your code is actually doing (49 % 2), rather than (1 % 2). You should use the int value for 1, not the ASCII char representation. – David.Jones Nov 28 '14 at 18:44
  • 1
    I have no clue what ASCII is :(... I thought my int number1 was the first digit of the actual number? – Louie Morris Nov 28 '14 at 18:46
  • The basic answer is, ASCII is the code used to represent characters. In Java, a char 1, isn't actually stored as a 1. It is stored as its ASCII value, which is 49. You should use int, rather than char, to store these values. – David.Jones Nov 28 '14 at 18:49
  • 1
    You are reading characters and '1' is a character just as 'A' or '#'. Each character has a code in the ASCII system (http://en.wikipedia.org/wiki/ASCII). You have to convert the character codes to their numerical meaning, if you want to use them as such. – Sentry Nov 28 '14 at 18:51
  • ok thank you :) Btw I upvoted your comment XD I don't know who downvoted it – Louie Morris Nov 28 '14 at 18:51
  • Exactly what @Sentry said. Thanks Louie Morris. I don't mind a down-vote, if its explained. What makes me mad is when arrogant users on here think that an answer isn't up to their standards, so they blindly down-vite without saying why. I wish SO made you comment before being able to down-vote. – David.Jones Nov 28 '14 at 18:53