0

Simple program prompts user to enter array size, then subsequently enter values.Then display sum, average,sum of odd and even numbers, highest and lowest number then displays Y/N try again prompt to restart or exit program. Try-catch for exceptions and Y/N try again prompt to restart or exit program.

import java.util.*;
public class oneDarray {
public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    float sum = 0,ave,oddsum=0,evesum=0,highnum=0,lownum=0;
    boolean prog=true;
        while(prog==true){
            try{
            System.out.println("Enter size of array");
            int arrSize = input.nextInt();
            if(arrSize>0 && arrSize<=25){
                 System.out.println("Enter array elements");
                 float numbers[] = new float [arrSize];
                for(int i=0; i<numbers.length;i++){
                numbers[i] = input.nextFloat();
                sum = sum + numbers[i];
            if(numbers[i]==0){
                highnum=numbers[i];
                lownum=numbers[i];
            }
            if(numbers[i]%2==0){
                evesum=evesum+numbers[i];
            }
            if(numbers[i]%2!=0){
               oddsum=oddsum+numbers[i];
            }
            if(i==0){
                lownum=numbers[i];
                highnum=numbers[i];
            }
            if(numbers[i]<lownum)
                lownum=numbers[i];
            else if(numbers[i]>highnum)
                highnum=numbers[i];      
    }
        ave= sum/numbers.length;
        System.out.println("The sum: "+sum);
        System.out.println("The average "+ave);
        System.out.println("odd sum "+oddsum);
        System.out.println("even sum "+evesum);
        System.out.println("highest num "+highnum);
        System.out.println("lowest num "+lownum);
        highnum=0;
        lownum=0;
        sum=0;
        ave=0;
        oddsum=0;
        evesum=0;
    }
    else{
        System.out.println("Invalid input");
    }
        System.out.println("Would you like to try again? Y/N");
        char YN = input.next().charAt(0);
        if(YN=='Y'||YN=='y'){
        prog=true;}
        else if(YN=='N'||YN=='n'){
        prog=false;
        System.out.println("Thank you for using the program, have a nice day");
        }
}catch(InputMismatchException ime){
    System.out.println("Invalid input");
    System.out.println("Would you like to try again? Y/N");
        char YN = input.next().charAt(0);
        if(YN=='Y'||YN=='y'){
        prog=true;}
        else if(YN=='N'||YN=='n'){
        prog=false;
        System.out.println("Thank you for using the program, have a nice day");
        }
        }
    }
    }
}

Copy pasted contents of try again prompt in catch, expected to answer Y/N prompt. display but skips prompt entirely and loops to start of program.

JVG_2002
  • 1
  • 1
  • 1
    If `nextInt()` causes an exception, the token is not consumed. Say I enter "20A". which isn't a number. Then the `input.next()` in your 'would you like to try agian?' prompt immediately returns with `20A`. If you want to ditch it, call `input.next();` without doing anything with it to 'consume' the invalid token. – rzwitserloot Apr 14 '23 at 02:24
  • You could also move your "yes/no" prompt outside the `try-catch` block, so you don't need to repeat the code – MadProgrammer Apr 14 '23 at 02:26

0 Answers0