0

Very Frustrated at my professor, because she did not teach try and catch concepts, neither did she teach us about throw exceptions either, so it is very difficult for me to do this program. The objective is to make a program where the user is asked to input an integer that prints "Hello World" that many times of the integer. The problem is I cannot check to make sure the user input is an integer. For instance, if the user chose to type a character or a double, how do I implement that into my code? And I cannot use throw exceptions or try and catch because we did not learn them yet.Thanks guys!!!

 import java.util.Scanner;

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

      System.out.println("Please enter an integer that shows the " + 
      "number of times to print \"Hello World\" : ");
      //store count
      number = scan.nextInt();

      System.out.print("Your integer is " + number);
      int remainder = number%1;
      int counts = 0;

      if( number>0 && remainder == 0)
      {
         while(counts <= number)
         {
            System.out.println("Hello World!");
            counts++;
            }
       }
       else
       System.out.print("Wrong, choose an integer!");

           }
         }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PatGreens
  • 129
  • 2
  • 10
  • 1
    Honestly, if your class is still at the point of inputting an integer and doing a simple loop, it makes sense that your professor hasn't taught you exception handling yet. I would suggest that your professor probably knows what she's doing, and she will be introducing exception handling in a later lesson. – Dawood ibn Kareem Jan 10 '14 at 23:50
  • She will, but I just feel it is frustrating when she gives us an assignment that would gladly need exception handling. – PatGreens Jan 11 '14 at 00:04

3 Answers3

2
scan.hasNextInt()

will check to see if the next value in the input stream is an integer.

as such:

int number = -1;

System.out.println("Please enter an integer that shows the " + 
  "number of times to print \"Hello World\" : ");
//store count
if (scan.hasNextInt()) number = scan.nextInt();

if (number != -1) System.out.print("Your integer is " + number);
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
2

You can use a loop and validate the input with a regex, like this:

Scanner scan = new Scanner(System.in);
String input = null;
while (true) {
    input = scan.nextLine();
    if (input.matches("\\d+")) {
        break;
    }
    System.out.println("Invalid input, please enter an integer!");
}
int number = Integer.parseInt(input);

This will keep asking for input until a valid integer is entered.

Keppil
  • 45,603
  • 8
  • 97
  • 119
1

And I cannot use throw exceptions or try and catch because we did not learn them yet.

For a first attempt, you could create a method that accepts a String as parameter. You will loop through all the chars of this String and check if each char is a digit. While this method returns false, re-ask the user for a new input.

Then use Integer.valueOf to get the int value..

public static boolean isNumber(String input)
  1. You will have to use sc.nextLine() to get the input
  2. The method Character.isDigit and toCharArray() will be useful
Alexis C.
  • 91,686
  • 21
  • 171
  • 177