0

New to programming here. I need to write application that does the following... Squaring application instructions

The code I have so far follows. I am running into a problem where my code will not read from negative integers to strings and properly prompt the user to enter valid data. I believe I need to nest my loops but am having trouble doing so. Any help would be greatly appreciated. Thanks.

import java.util.Scanner;

public class Squaring {

    public static int getValidInt(int greaterThan, Scanner scan) {
        System.out.println("Enter an integer greater than " + greaterThan + ":" );
        int input; 

        while ( !scan.hasNextInt() ) { 
            String garbage = scan.next();
            scan.nextLine(); 
            System.out.println(garbage + " is not valid input."); 
            System.out.println("Enter an integer greater than " + greaterThan + ":" );
        }

        while ( !((input = scan.nextInt()) > greaterThan )) { 
            int garbage = input; 
            System.out.println(garbage + " is not greater than 1.");
            System.out.println("Enter an integer greater than " + greaterThan + ":" );
        }

        return input; 
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = 1 ;
        int total = 0;
        a = getValidInt(a,scan);
        int b = a; 
        System.out.println(a);
        long n = a; 
        while ( n < 1000000 ) {
            System.out.println(n*n);
            n = n * n; 
            total = total + 1; 
        }
        System.out.println(b + " exceeded 1,000,000 after " + total + " squarings.") ;
    }
}
Quinn
  • 7,072
  • 7
  • 39
  • 61
Nicholas
  • 89
  • 6

2 Answers2

1

Without any try-catch:

public static int getValidInt(int greaterThan, Scanner scan) {
    int input = 0;
    boolean valid = false;

    while (!valid) {
        System.out.println("Enter an integer greater than " + greaterThan + ":");

        if (!scan.hasNextInt()) {
            String garbage = scan.next();
            System.out.println(garbage + " is not valid input.");
        } else {
            input = scan.nextInt();

            if (input <= greaterThan) {
                System.out.println(input + " is not greater than " + greaterThan);
            } else {
                valid = true;
            }
        }
    }

    return input;
}

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

    int a = getValidInt(1, scan);
    System.out.println(a);

    int total = 0;
    long n = a;

    while ( n < 1000000 ) {
        n = n * n;
        System.out.println(n);
        total++;
    }

    System.out.println(a + " exceeded 1,000,000 after " + total + " squarings.") ;
}
forpas
  • 160,666
  • 10
  • 38
  • 76
0

I've come up with the following:

public static int getValidInt(int greaterThan, Scanner scan) {

    int number = greaterThan;

    do {
        while (!scan.hasNextInt()) {
            String garbage = scan.next();
            System.out.println(garbage + " is not valid input.");
        }
        number = scan.nextInt();

        if (number < greaterThan) {
            System.out.println("input is: " + number + " minimum is: " + greaterThan);
        }

    } while (number < greaterThan);

    return number;
}

It has a do while loop which makes sure number cannot be smaller than greaterThan, if it is it will do the while loop again. Inside the do while loop is another loop which tells the user that their input is garbage, and will continue doing so until a number is inserted.

Mark
  • 5,089
  • 2
  • 20
  • 31