0

I am learning java in the internet in sites like "oracle academy" and using Google to search how to do somethings, i wanted to make a simple java program that takes an number(day) a number or word(month) and another number(year), so if you input something like

"3" "1" and "1993"  

it outputs

"January 1st, 1993"  

and if you input something like

"2" "July" and "1992"  

it outputs

"7/2/1992"  

I kind of already know how to make it using "case" and while loops to tell you if you input something incorrectly, but on the "day" part i tried using a while loop to keep asking you to input something if the input wasn't a number between 1 and 31 but i can't make it return the number from the while loop using the return command, is there any way to return the number without using the return command or not? the code:

public static void main(String[] args) {  
    String x;  
    Scanner scan = new Scanner(System.in);  
        //day  
        System.out.println("Insert Day");  
        while (1 < 2){  
        x = scan.nextLine();  
        if (x.matches(".*\\d.*")){  
            q = Integer.parseInt(x);  
            if ((0 < q) && (q < 32)){  
                return q;  
        }  
            else {  
            System.out.println("please use a valid number");       
        }  
        }  
        else {  
            System.out.println("please use a number");  
        }  
        }  
        System.out.println(q);  
2
  • This won't work, you can still have the wrong number of days in a month (31 in Feb for example), you need to use SimpleDateFormat. Commented Jul 11, 2013 at 23:00
  • I was going to make it so you need to input the day 1st but then the number of days would be wrong sometimes tough, I'll make the month input 1st so you can't have the wrong number of days on certain months, thanks for letting me know. Commented Jul 12, 2013 at 0:10

2 Answers 2

3

You can use return if you refactor this into a separate function:

public static int getDay() {
    Scanner scan = new Scanner(System.in);
    System.out.println("Insert Day");
    while (true){  
        line = scan.nextLine();  
        if (line.matches(".*\\d.*")){
            int day = Integer.parseInt(line);  
            if (0 < day && day < 32){  
                return day;  
            } else {  
                System.out.println("please use a valid number");       
            }  
        } else {  
            System.out.println("please use a number");  
        }  
    }
}

public static void main(String[] args) {
    int day = getDay();
    System.out.println(day);  
}

Or you could use a break:

Scanner scan = new Scanner(System.in);
System.out.println("Insert Day");
int day = -1;
while (true){  
    line = scan.nextLine();  
    if (line.matches(".*\\d.*")){
        day = Integer.parseInt(line);  
        if (0 < day && day < 32){
            break;
        } else {  
            System.out.println("please use a valid number");       
        }  
    } else {  
        System.out.println("please use a number");  
    }  
}

Or you could use your while condition:

int day = -1;
Scanner scan = new Scanner(System.in);
System.out.println("Insert Day");
while (day < 1 || day > 31){  
    line = scan.nextLine();  
    if (line.matches(".*\\d.*")){
        day = Integer.parseInt(line);  
        if (day < 1 || day > 31){
            System.out.println("please use a valid number");       
        }  
    } else {  
        System.out.println("please use a number");  
    }
}

Also, you can simplify your code by using the NumberFormatException parseInt throws, instead of trying to validate it yourself with a regular expression:

int day = -1;
Scanner scan = new Scanner(System.in);
System.out.println("Insert Day");
while (day < 1 || day > 31){
    line = scan.nextLine(); 
    try { 
        day = Integer.parseInt(line);
        if (day < 1 || day > 31){
            System.out.println("please use a valid number");       
        }  
    } catch (NumberFormatException e) {
        System.out.println("please use a number");  
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for a complete answer, but you have a minor bug in your example that uses the while condition. 0 and 32 are never a valid values for a day, so you should use day >= 32 instead of day > 32 and similarly use <= when comparing to 0.
but <= is for bigger or equal, so "<= 0" would mean "bigger or equal to 0" and would allow the "day" int to be 0, if you used "<=" you should use "<= 1" so it would only accept 1 or more instead of 0 or more.
1

The return statement returns a value from the entire method you're in. But you're in main, which can't return a value (it's void).

If you want to end the while loop when you have a good value, then use the break statement to break out of the while loop. Control then passes to the next statement following the end of the loop.

1 Comment

I would suggest using methods rather than breaking. Best teach good habits early...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.