2

I'm new Java and programming in general. I'm stuck on a problem in a course I'm taking and any help would be appreciated. We're covering catch blocks and the program needs to read two integers on the same line and divide the two. The two catch blocks are dividing by zero and not entering a number. The problem I'm running into, i cannot get the program to correctly read the two integers input.

package chapter9problem2;

import java.util.Scanner;

public class Chapter9Problem2 {


    public static void main(String[] args) {


        Scanner keyboard = new Scanner(System.in);

        boolean done = false;

        while (!done)
        {

            try{

                System.out.println("Enter two numbers. Please leave a space between the numbers. I will compute the ratio.");
                String input = keyboard.nextLine();
                String[] numbersStr = input.split(" ");
                int[] numbers = new int[ numbersStr.length ];

                for ( int i = 0; i < numbersStr.length; i++ )
                {
                    numbers[i] = Integer.parseInt( numbersStr[i] );
                }


                System.out.println("");
                System.out.println("The ratio r is: "+(numbers[1]/numbers[2]));
            }
            catch (ArithmeticException e)
            {
                System.out.println("There was an exception: Divide by zero... Try again.");
            }
            catch (Exception e) {

                System.out.println("You must enter an Integer. ");
            }
            continue;
        }

    }

}

And this is the result

G Orwell
  • 21
  • 2
  • Not the error but you do not need a `continue` at the end of your loop. – Diasiare Apr 27 '18 at 14:17
  • I understand this is probably not in the scope of your coursework, but I'd like to point out what you are doing is known as error hiding (https://en.wikipedia.org/wiki/Error_hiding). In short, it means that you caught an exception, but didn't log the cause of the issue. When debugging, it can save you a lot of time and headaches if you remember to log your error messages. For your coursework, try throwing e.printStackTrace() in your catch blocks when debugging, or check out your IDEs debugger capabilities to get a bit more insight into what's going wrong. – Nick DeFazio Apr 27 '18 at 14:22

3 Answers3

3

Arrays are indexed from 0, not 1. So to get the first element of an array you must access element 0 (so numbers[0] in your case). Therefor this line

System.out.println("The ratio r is: "+(numbers[1]/numbers[2]));

Should read

System.out.println("The ratio r is: "+(numbers[0]/numbers[1]));

Also note that integer division rounds. So from the example you posted the result of dividing 10 by 20 would be 0. This is probably not what you want since you are using the term ratio. To get a true ratio you need to cast one of the numbers to double. The above line then becomes

System.out.println("The ratio r is: "+((double) numbers[0]/numbers[1]));

This question has more details.

Diasiare
  • 745
  • 1
  • 6
  • 18
2

The problem here is that you are trying to access number[1] and number[2].

It should be number[0] and number[1]

System.out.println("The ratio r is: "+(numbers[0]/numbers[1]));
Garima
  • 121
  • 4
0
System.out.println("The ratio r is: "+(numbers[0]/numbers[1]));

This is correct code you have written indexes as 1 and 2 so it's going out of bounds.

Vishnu
  • 89
  • 1
  • 7