0

So basically the code is a program that solves the necklace program (two number are added and returns only the ones digit. This process is repeated until the original two numbers are returned by the program.

I need to error proof it so that strings and other non-integer data can be input by the user. If you have any other way that works, feel free to suggest it.

//import statements
    import java.util.Scanner;
    import java.util.InputMismatchException;


    //class assingment statement
    public class NecklaceProgram  {

    //method assinmgent statement
    public static void main(String [] args)

    {
    //variables used throughout program
    int num1;
    int num2;
    int count;

    //Scanner input statement
    Scanner input = new Scanner (System.in);



    try {

    //user input for first integer
    System.out.print("Enter first single digit integer : "); 
    num1 = input.nextInt(); 

    //user input for second integer
    System.out.print("Enter second single digit integer : "); 
    num2 = input.nextInt(); 



    } catch (InputMismatchException e) 
    {
      System.out.println("Please enter only a single digit integer");
    }



    //variables for next numbers in sequence
    int nextNumber;
    int nextNextNumber;

    //calculation for next two numbers in sequence (third and fourth number)
    nextNumber = (num1 + num2) % 10;
    nextNextNumber = (num2 + nextNumber) % 10;
    count =2;

    //output of first 4 numbers in string
    System.out.print (num1 + " " + num2 + " " + nextNumber + " " + nextNextNumber);

    /*true or false statement stating that the program will be "finished" when the first two integers 
     entered by the user are the last two integers inthe sequence
     */
      boolean finished = (num1 == nextNumber) && (num2 == nextNextNumber);

    //while statement that loops through the production of the sequence until the boolean condition is met
    while (!finished)

      {
           //statements that calculate the next numbers in the sequence
           nextNumber = (nextNumber + nextNextNumber)%10;
           nextNextNumber = (nextNextNumber + nextNumber)%10;

           //java outputs the next two numbers in the seuqence until the boolean condition is met
           System.out.print(" " + nextNumber + " " + nextNextNumber);

           //the program stops running when the boolean condition is met
           //this is when the first two numbers input by the user are equal to the last two numbers in the sequence
           finished = (num1 == nextNumber) && (num2 == nextNextNumber); 

           //this counter is used to determine the number of steps taken to       output the original two numbers
           //this value will be output at the end of the program
           count += 2;

           }

         System.out.println("\nThis process took " + count + " steps to be completed");
       }
      }
  • Well, suppose that the first `nextInt()` throws an exception... you're catching the exception, but never assigning a value to `num1` or `num2` because you don't reach that part of the `try` block... – Jon Skeet Nov 11 '15 at 21:09
  • You Aren't Getting This Error After Importing InputMismatchException. The Two Matters Are Unrelated. – user207421 Nov 11 '15 at 22:36

0 Answers0