having trouble getting a valid int in java
Go to solution
Solved by edwardsdean,
i managed to fix it after much frustration, by changing the get_choice()
the code for thoes interested
import java.io.File;import java.util.Scanner;public class Age_Statistics { public static void main(String[] args) { int choice; program_loop: while (true) { display_menu(); choice = get_choice(); switch (choice) { case 1: System.out.format("There are "); System.out.print(count_ages()); System.out.println(" ages in the file.\n"); break; case 2: System.out.print("The average age is "); System.out.print(average_age()); break; case 3: System.out.print("The Minimum age is "); System.out.println(find_minimum_age()); break; case 4: System.out.print("The Minimum age is "); System.out.println(find_maximum_age()); break; case 5: break program_loop; } } } private static double find_maximum_age() { try { double max_age = 0; Scanner input = new Scanner(new File("data.txt")); while (input.hasNextDouble()) { double age = input.nextDouble(); if (age > max_age) { max_age = age; } } input.close(); return max_age; } catch (Exception e) { return -1; } } private static double find_minimum_age() { try { double min_age = 1000; Scanner input = new Scanner(new File("data.txt")); while (input.hasNextDouble()) { double age = input.nextDouble(); if (age < min_age) { min_age = age; } } input.close(); return min_age; } catch (Exception e) { return -1; } } private static float average_age() { try { File file = new File("data.txt"); Scanner input = new Scanner(file); int count = 0; float total = 0; while (input.hasNext()) { String line = input.next(); ++count; total += Integer.parseInt(line); } float average = 0; average = total / count; input.close(); return average; } catch (Exception e) { return -1; } } private static int count_ages() { int count = 0; try { File file = new File("data.txt"); Scanner input = new Scanner(file); while (input.hasNext()) { String line = input.next(); ++count; } input.close(); return count; } catch (Exception e) { return -1; } } private static int get_choice() { int choice = 0; Scanner input = new Scanner(System.in); System.out.println("Choice: "); do { while (!input.hasNextInt()) { input.next(); System.out.println("That's not a valid input"); System.out.println("Choice: "); } choice = input.nextInt(); input.nextLine(); // added to fix } while (choice < 1 || choice > 5); return choice; } private static void display_menu() { System.out.println("Age Statistics (Version: 1.0)"); System.out.println("Options:"); System.out.println("\t1. Tally the number of ages"); System.out.println("\t2. Compute the average age"); System.out.println("\t3. Find the minimum age"); System.out.println("\t4. Fine the maximum"); System.out.println("\t5. Exit"); }}
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now