Home > Blockchain >  Working on a Java Program that utilizes For Loops to separate Even, Odd, and Negative Inputted Integ
Working on a Java Program that utilizes For Loops to separate Even, Odd, and Negative Inputted Integ

Time:02-26

I am working on a Java program that determines Evens, Odds, and Negative numbers from 12 inputted integers.

It then separates them into different arrays. The course I am following suggests building an exception handler and I utilized the Try-Catch Method for the Exception error I may receive.

It then creates an Out of Bounds error for counting these numbers when I enter a String.

I've commented on the area in which I have trouble reprompting the user. So far, I've tried prompting at the error with twelveInt [i] = in.nextInt(); and just in.next();.

Why is the program affected outside of the loop?

Here is the program:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
          
    int [] twelveInt = new int [12];
          
    int countEven = 0;
          
    int countOdd = 0;
          
    int countNeg = 0;
    
    boolean ehandle = true;
   

    for (int i = 0; i < twelveInt.length; i  ) {
        while(ehandle){
            try{
                System.out.println("Enter the #"   (i   1)   " integer.");
                twelveInt [i] = in.nextInt();
                ehandle = false;
            }
            catch(Exception e){
                System.out.println("Please enter integers only");
                // Unsure of what to add here to handle the errors and allow me to reprompt. in.next(); 
                //does not work nor does twelveInt [i] = in.nextInt();
            }
            if (twelveInt[i] % 2 == 0){
                countEven  ;
            }
            if (twelveInt[i] % 2 != 0){
                countOdd  ;
            }
            if (twelveInt[i] < 0){
                countNeg  ;
            } 
        }
    }
        
    int [] evens = new int [countEven];    
    int [] odds = new int [countOdd];
    int [] negatives = new int [countNeg];
    
    countEven = 0;
    countOdd = 0;
    countNeg = 0;
        
    
    for (int i : twelveInt) {
        if (i % 2 == 0){
            evens[countEven  ] = i;
        }
        if (i % 2 != 0){
            odds[countOdd  ] = i;
        }
        if (i < 0){
            negatives[countNeg  ] = i;
        }          
    }
              
    System.out.println("Here are the Even numbers you entered");
    System.out.println(Arrays.toString(evens));
          
    System.out.println("Here are the Odd numbers you entered");
    System.out.println(Arrays.toString(odds));
          
    System.out.println("Here are the Negative numbers you entered");
    System.out.println(Arrays.toString(negatives));

CodePudding user response:

The problem in your code is that after you run your loop and enter your first number, you then define ehandle = false.

This means that you never enter the rest of your loop since you exit the while loop. Therefore, you never increment the values of countEven, countOdd, or countNeg.

This results in an error because when you try running this line: int [] evens = new int [countEven]; , countEven is still 0, so you get an error when you try to iterate it.

To avoid this error, you can modify your try catch error as so:

import java.util.*;
import java.io.*;
class Main {
  public static void main(String[] args){
    Scanner in = new Scanner(System.in);
          
    int [] twelveInt = new int [12];
          
    int countEven = 0;
          
    int countOdd = 0;
          
    int countNeg = 0;
                 
    
          
    for (int i = 0; i < twelveInt.length; i  ) {
        System.out.println("Enter the #"   (i   1)   " integer.");
        boolean error = true;
        while (error) {
            try {
              twelveInt [i] = in.nextInt();
              if (twelveInt[i] % 2 == 0){
                  countEven  ;
              }
              if (twelveInt[i] % 2 != 0){
                  countOdd  ;
              }
              if (twelveInt[i] < 0){
                  countNeg  ;
              } 
              error = false;
            } 
            catch (Exception e) {
              System.out.println("Please enter integers only");
              in.next();
            }
        }
    }
    
    int [] evens = new int [countEven];    
    int [] odds = new int [countOdd];
    int [] negatives = new int [countNeg];
        
    
    countEven = 0;
    countOdd = 0;
    countNeg = 0;
    
        
    
    for (int i : twelveInt) {
            if (i % 2 == 0){
                evens[countEven  ] = i;
            }
            if (i % 2 != 0){
                odds[countOdd  ] = i;
            }
            if (i < 0){
                negatives[countNeg  ] = i;
            }          
        }
              
    System.out.println("Here are the Even numbers you entered");
    System.out.println(Arrays.toString(evens));
          
    System.out.println("Here are the Odd numbers you entered");
    System.out.println(Arrays.toString(odds));
          
    System.out.println("Here are the Negative numbers you entered");
    System.out.println(Arrays.toString(negatives));
  }
}

We first try taking in the user input, and if we get an exception error, we will use the catch so our code does not terminate. NOTE: we must do in.next() otherwise we will get an infinite loop (since integers leave a trailing newline, resulting in infinite exceptions).

I hope this helped! Please let me know if you need any further clarifications or details :)

CodePudding user response:

If you want to reiterate your while(ehandle) loop when you get an exception, you can put continue in your catch block. But you should set ehandle back to true at the top of your for-loop.

for (int i = 0; i < twelveInt.length; i  ) {
    ehandle = true;
    while (ehandle) {
        try {
            System.out.println("Enter the #"   (i   1)   " integer.");
            twelveInt[i] = in.nextInt();
            ehandle = false;
        } catch(Exception e) {
            System.out.println("Please enter integers only");
            continue;
        }
        ...
  •  Tags:  
  • java
  • Related