HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Two integers start and end

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
starttwoendandintegers

Problem

Write a program that reads two integers start and end from the user
and prints the number from start to end inclusive. However, If end was
less than start than you should print them in descending order.

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);

    int start = scan.nextInt();
    int end = scan.nextInt();

    if(start  end)
        for(int i = start; i >= end; i--)
            System.out.print(i + " ");  
}

Solution

Input checking

The most obvious issue that I see is that you are not verifying that the correct input is being entered. For example, if you enter a String instead of an int:

> hello
Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Scanner.java:864)
  at java.util.Scanner.next(Scanner.java:1485)
  at java.util.Scanner.nextInt(Scanner.java:2117)
  at java.util.Scanner.nextInt(Scanner.java:2076)
  at Main.main(Main.java:8)


A natural way to do this is with Scanner.hasNextInt(). Also, consider adding a little bit of feedback so it is less confusing for the user. Something like this, for example:

Scanner scan = new Scanner(System.in);
    int start;
    int end;

    System.out.println("Enter 1st whole number: ");
    while (!scan.hasNextInt()) {
        System.out.println("Input must be a whole number. Try again:");
        scan.next();
    }
    start = scan.nextInt();

    System.out.println("Enter 2nd whole number: ");
    while (!scan.hasNextInt()) {
        System.out.println("Input must be a whole number. Try again:");
        scan.next();
    }
    end = scan.nextInt();


Result:

Enter 1st whole number: 
> hello
Input must be a whole number. Try again:
> 5
Enter 2nd whole number: 
> world
Input must be a whole number. Try again:
> 20
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


One other (more general) method (though it is more of an anti-pattern in this particular case) to validate things is with a try/catch block. See this answer on Stack Overflow for an example on how to do this.

Scanner scan = new Scanner(System.in);
    int start;
    int end;

    try { 
        System.out.println("Enter 1st number:");
        start = scan.nextInt();
        System.out.println("Enter 2nd number:");
        end = scan.nextInt();

    } catch (java.util.InputMismatchException e) {
        System.out.println("Input must be an integer.");
        System.out.println("Exiting program.");
        return;
    }


Enter 1st number:
> hello
Input must be an integer.
Exiting program.


Formatting

One other remark, although it is more stylistic, it is often considered a good habit in general to include braces in Java even when they are not needed. So this bit:

if(start  end)
    for(int i = start; i >= end; i--)
        System.out.print(i + " ");


Would look like this:

if(start  end) {
        for(int i = start; i >= end; i--) {
            System.out.print(i + " ");  
        }
    }

Code Snippets

> hello
Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Scanner.java:864)
  at java.util.Scanner.next(Scanner.java:1485)
  at java.util.Scanner.nextInt(Scanner.java:2117)
  at java.util.Scanner.nextInt(Scanner.java:2076)
  at Main.main(Main.java:8)
Scanner scan = new Scanner(System.in);
    int start;
    int end;

    System.out.println("Enter 1st whole number: ");
    while (!scan.hasNextInt()) {
        System.out.println("Input must be a whole number. Try again:");
        scan.next();
    }
    start = scan.nextInt();

    System.out.println("Enter 2nd whole number: ");
    while (!scan.hasNextInt()) {
        System.out.println("Input must be a whole number. Try again:");
        scan.next();
    }
    end = scan.nextInt();
Enter 1st whole number: 
> hello
Input must be a whole number. Try again:
> 5
Enter 2nd whole number: 
> world
Input must be a whole number. Try again:
> 20
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Scanner scan = new Scanner(System.in);
    int start;
    int end;

    try { 
        System.out.println("Enter 1st number:");
        start = scan.nextInt();
        System.out.println("Enter 2nd number:");
        end = scan.nextInt();

    } catch (java.util.InputMismatchException e) {
        System.out.println("Input must be an integer.");
        System.out.println("Exiting program.");
        return;
    }
Enter 1st number:
> hello
Input must be an integer.
Exiting program.

Context

StackExchange Code Review Q#108495, answer score: 5

Revisions (0)

No revisions yet.