Jump to content

having trouble getting a valid int in java

edwardsdean
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");    }} 

as in the title i can seem to get a valid int from a scanner

 

    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();        } while (choice < 1 | choice > 4);            input.close();            return choice;
Link to comment
Share on other sites

Link to post
Share on other sites

while (choice < 1 | choice > 4)
Should be
while (choice < 1 || choice > 4)
But even without changing that it has no problem reading in ints for me.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

while (choice < 1 | choice > 4)
Should be
while (choice < 1 || choice > 4)
But even without changing that it has no problem reading in ints for me.

 

when i put in 1 it give me this exception

 

Exception in thread "main" java.util.NoSuchElementException    at java.util.Scanner.throwFor(Unknown Source)    at java.util.Scanner.next(Unknown Source)    at Age_Statistics.get_choice(Age_Statistics.java:101)    at Age_Statistics.main(Age_Statistics.java:9)
Link to comment
Share on other sites

Link to post
Share on other sites

Could you post the rest of the code, I can't find anything wrong with what you posted.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

here is the rest of the code :)

 

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();        } while (choice < 1 || choice > 4);            input.close();            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");    }}

Link to comment
Share on other sites

Link to post
Share on other sites

So the problem is that calling close on a scanner also closes the stream it is wrapping (System.in). To fix it you can create a global static scanner object or create one in main() and pass it to any function that needs it then only close it after you break from the while loop in main.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

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");    }} 

Link to comment
Share on other sites

Link to post
Share on other sites

--snip--

Not quite, the problem went away because you got rid of the input.close() line.

I'm not really sure how Java handles unclosed streams (other than Eclipse complaining about them) so that may end up causing other problems.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Not quite, the problem went away because you got rid of the input.close() line.

I'm not really sure how Java handles unclosed streams (other than Eclipse complaining about them) so that may end up causing other problems.

it doesn't seem to be causing any other problems

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×