Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:35:58+00:00 2026-06-04T09:35:58+00:00

public static void readStaffsFromFile() { String inFileName = startup.txt; int numStaff, staffID; String name,

  • 0
 public static void readStaffsFromFile() {
    String inFileName = "startup.txt";
    int numStaff, staffID;
    String name, address;
    Staff newStaff;
    boolean fileExists;
    Scanner inFile = null;
    File databaseFile = new File(inFileName);

    fileExists = databaseFile.exists();

    if (fileExists) {
        try {
            inFile = new Scanner(databaseFile);
        } catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(null, "The file startup.txt has just now been deleted.");
            return; // cannot do anything more.
        }

        numStaff = inFile.nextInt();
        inFile.nextLine();

        for (int i = 0; i < numStaff; i++) {
            staffID = inFile.nextInt();
            name = inFile.nextLine();
            address = inFile.nextLine();

            // try{
            newStaff = new Staff(staffID, name, address);
            addStaff(newStaff);
            // } catch (StaffException se)
            // {
            // System.out.println("Unable to add staff: " + name +
            // " to the system.");
            // }

        }

    }
    JOptionPane.showMessageDialog(null, "System has been set up with default data from startup.txt.");
}

I have this method and when I try to call this method from main, it gives me this error.

Exception in thread “main” java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at SystemStartUp.readStaffsFromFile(SystemStartUp.java:195)
at SystemStartUp.loadFromFile(SystemStartUp.java:160)
at StartUp.main(StartUp.java:9)

The error line of error states that my error starts from the line of “staffID = inFile.nextInt();“

The input file looks like this.

13

11111111

Chris Ling

999 Dandenong Road

22222222

Des Casey

100 Silly Drive

33333333

Maria Indrawan

90 Programming Road

44444444

Campbell Wilson

2/5 Database Street

55555555

Janet Fraser

21 Web Drive

66666666

Judy Sheard

34 Hos Road

77777777

Ngoc Minh

24 Message Street

88888888

Martin Atchinson

45 Martine Street

99999999

Aleisha Matthews

1/6 Admin Road

10101010

Denyse Cove

100 Reception Street

12121212

Cornelia Liou

232 Reception Road

23232323

Trudi Robinson

111 Manager Street

34343434

Henry Linger

2/4 HDR Street
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:35:59+00:00Added an answer on June 4, 2026 at 9:35 am

    Probably the staffID doesn’t always contains numbers. Please check the input file.

    After staffID, you have to add inFile.nextLine(); to consume the new line character after of the line with number. Otherwise, you will get the error on the second loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void fillCheckList(string ListType,int RecordNum,CheckBox chkRequired,TextBox txtComplete,TextBox txtMemo) { string sql_Check = String.Format(@SELECT
public class Sample { public static void main(String[] args) { int year = 2012;
public class I { public static void main(String args[]) { int i=0,j=1,k=2; i=i++ -
public static void main(String[] args) { staples[] stemp = new staples[8]; int j; try
public static void main(String[] args) { int [][]shatner = new int[1][1]; int []rat =
public static void main(String[] args) { int x = 1 + + + +
public static void ejemplosString(String palabra){ char[] letras = palabra.toCharArray(); int contadorVocales = 0; for
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public static void main(String[] args) { System.out.println(fun(2,3,4)); } static int fun(int a,int b,int c)
public static void main (String[] args) { System.out.println(factorial(5)); } public int factorial(int n) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.