Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program bellow works just fine on java, the problem is, if a user eneters a number which is not an integer( 3.1, 5.66) I

The program bellow works just fine on java, the problem is, if a user eneters a number which is not an integer( 3.1, 5.66) I end up getting a run time error. I need some one to help me fix this problem so that if a user eneters a number that is not an interger like 3.4, 6.2....a message should be displayed on the screen which says "you need to eneter an integer please". instead of a run time error. this message should be displayed until an integer is entered and then should continue.

import java.util.*;

class ListStats { //Scanner class object static Scanner keyboard = new Scanner(System.in); //Main method public static void main(String args[]) { //Size int maxSize = 100; //Creating an array int[] list = new int[maxSize]; int listSize, searchValue, index, i; //Reading numbers into array listSize = getNumbers(list); //Printing count System.out.printf(" As a total %d numbers were read. ", listSize); //Smallest number System.out.printf(" Smallest Number: %d ", list[0]);

//Largest number System.out.printf(" Largest Number: %d ", list[listSize-1]); //Median System.out.printf(" Median: %.2f ", calculateMedian(list, listSize)); //Average System.out.printf(" Average: %.2f ", calculateAverage(list, listSize)); String ans; do { //Reading search value System.out.print(" Enter a value to search: "); searchValue = keyboard.nextInt(); //Finding index index = search(list, listSize, searchValue); //Not found if(index == -1) { System.out.println(" Element not found in the list... "); } //Found else { //Printing index System.out.println(" Element found at index: " + index + " "); } //Prompting user System.out.print(" Do you want to search more (Y/N): "); ans = keyboard.next(); }while(ans.equalsIgnoreCase("Y")); System.out.println(" "); } //Method that reads numbers from user public static int getNumbers(int[] list) { int i=0, val=0; //Iterate till user enters a negative value or max size is reached while(val>=0) { //Prompting for number System.out.print(" Enter a number: "); val = keyboard.nextInt(); if(val > 0) { //Placing number in array list[i] = val; //Incrementing number of numbers entered i++; //Checking for max size if(i>=100) { //Printing error message System.out.println(" Maximum Size reached... "); val = -1; } } } //Return number count return i; } //Method that calculates median public static double calculateMedian(int[] array, int len) { double median; //Even length if (len % 2 == 0) { median = ((double)array[len/2] + (double)array[len/2 - 1])/2; } //Odd length else { median = (double) array[len/2]; } return median; } //Method that calculates average of numbers public static double calculateAverage(int[] array, int size) { int i, sum=0; double avg; //Iterating over array for(i=0; i { //Accumulating sum sum = sum + array[i]; } //Calculating average avg = sum / (double)size; //Return average return avg; } //Method that searches for a value public static int search(int[] array, int size, int value) { int i, index = -1; boolean found = false; //Iterating over array for(i=0; i { //Searching for value if(found == false && array[i] == value) { //Updating position index = i; found = true; } } //Return index where element is found return index; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

Do you currently have a team agreement?

Answered: 1 week ago

Question

c. How is trust demonstrated?

Answered: 1 week ago