0

My java program here is expected to take the user input in my getState() method but instead, it skips right along to the next method and throws an error as a result. I have tried many different thing and nothing seems to be working

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class main {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        try {
            System.out.println("Welcome to our survey. You may enter \"quit\" at any time to cancel the survey.");
            int age = getAge();
            String[][] states = readStateFile();
            String state = getState(states);
            int ZIPCode = getZIPCode();
            System.out.printf("\nAge:\t\t%d\n", age);
            System.out.printf("Address:\t%s %s\n\n", ZIPCode, state);
            System.out.println("Your survey is complete!");
        } catch (CancelledSurveyException e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Thank you for your time.");
        }
    }

    public static int getAge() throws CancelledSurveyException {

        int age = 0;
        try {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter age: ");
            while (age < 18) {

                age = keyboard.nextInt();

                if (age < 18) {
                    System.out.println("You are too young to be taking this survey!");
                    throw new CancelledSurveyException(null);
                }

            }
            keyboard.close();

        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
        return age;
    }

    public static String[][] readStateFile() throws FileNotFoundException, IOException {
        String[][] states = new String[50][2];
        DataInputStream dis = new DataInputStream(new FileInputStream("states.bin"));
        try {
            int row = 0;
            while (true) {
                String abbreviation = dis.readUTF();
                String name = dis.readUTF();
                states[row][0] = abbreviation;
                states[row][1] = name;
                row++;
            }
        } catch (EOFException e) {
            // end of file reached
        } finally {
            dis.close();
        }
        return states;
    }

    public static String getState(String[][] states) throws CancelledSurveyException {
        Scanner scanner1 = new Scanner(System.in);
        try {
            System.out.println("Enter your state abbreviation: ");
            String abbr = scanner1.nextLine();
            for (int i = 0; i < states.length; i++) {
                if (states[i][0].equalsIgnoreCase(abbr)) {
                    return states[i][1];
                }
            }
            throw new CancelledSurveyException("Invalid state abbreviation.");
        } finally {
            scanner1.close();
        }
    }

    public static int getZIPCode() throws CancelledSurveyException {
        Scanner scanner = new Scanner(System.in);
        try {
            while (true) {
                System.out.print("Please enter your ZIP code or 'quit' to quit: ");
                String input = scanner.nextLine().trim();
                if (input.equalsIgnoreCase("quit")) {
                    throw new CancelledSurveyException("Your survey was cancelled.");
                }
                try {
                    int zipCode = Integer.parseInt(input);
                    if (zipCode < 10000 || zipCode > 99999) {
                        System.out.println("Invalid ZIP code.");
                    } else {
                        return zipCode;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Invalid input. Please enter a number.");
                }
            }
        } finally {
            scanner.close();
        }
    }
}

My java program here is expected to take the user input in my getState() method but instead, it skips right along to the next method and throws an error as a result. I have tried many different thing and nothing seems to be working

  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – tgdavies Mar 16 '23 at 00:13
  • Welcome to our survey. You may enter "quit" at any time to cancel the survey. Enter age: 20 Enter your state abbreviation: Exception in thread "main" Thank you for your time. java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at main.main(main.java:16) – Anonymous877959 Mar 16 '23 at 00:15
  • 2
    Also you are closing system.in. Just create a single Scanner in `main` and pass it to the functions which use it. Never close System.in. – tgdavies Mar 16 '23 at 00:21

0 Answers0