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

  • Home
  • SEARCH
  • 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 3223316
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:05:34+00:00 2026-05-17T16:05:34+00:00

Looking for help with the following code: package pkgPeople; import java.io.File; import java.io.FileOutputStream; import

  • 0

Looking for help with the following code:

package pkgPeople;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class RetrieveNonSerializedFile {

    public static void main(String[] args) throws Exception{
    File inFile = new File("F:/CS9.27/friends2.dat");

       Scanner reader = new Scanner(inFile);


       while (reader.hasNextLine()){
        String nm = reader.nextLine();
        int height = reader.nextInt();
        int weight = reader.nextInt();
        double balance = reader.nextDouble();
        long acctID = reader.nextInt();

        System.out.println(nm + ":" + height + " inches " + weight + " pounds" + acctID + " account ID" + balance + "dollars");
        /*writer.println(nm);
        writer.println(height);
        writer.println(weight);
        writer.println(acctID);
        writer.println(balance);*/
       }

        reader.close();
        //writer.close();




    }

}

When running the program, the exception

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at pkgPeople.RetrieveNonSerializedFile.main(RetrieveNonSerializedFile.java:22)

is thrown.

The friends2.dat is a data file that has the following…..

Jim is the name of the person.
13 inches is the height of Jim.
14 pounds is the weight of Jim
1234.650000 dollars is the balance of Jim
12345 is the ID of the bank account.

It is just a text file. Any help to get through the InputMismatch would be great. Thanks.

  • 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-05-17T16:05:35+00:00Added an answer on May 17, 2026 at 4:05 pm

    Edit: never mind. i misunderstood the pattern of the inputfile 🙂

    The Scanner reads on calling String nm = reader.nextLine(); the complete line. Then you call reader.nextInt(); but the next line doesn’t start with a integer. Therefore it throws a Exception.

    I think you should try to use a regular expression (http://download-llnw.oracle.com/javase/6/docs/api/java/util/Scanner.html#next%28java.util.regex.Pattern%29) or use (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf%28java.lang.String%29) for finding the correct parts.

    BTW the homework tag should be added 🙂

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

Sidebar

Related Questions

Hi I'm looking for some help in mapping the following tables to a hibernate
I'm looking for help setting a new default property value for an inherited control
I'm looking for help getting started working programmatically with audio. Specifically, the platform I'm
This might be too opinionated a question, but looking for help! I have been
Looking for some help with a Labview data collection program. If I could collect
I'm looking for some help on my discussion. We're discussing two solutions to a
I'm looking for some help with a dependency issue. In a nutshell, I've included
HI, I am new to the scrum methodology and looking for some help to
Looking for a bit of regex help. I'd like to design an expression that
I'm looking for a little help on programmatically passing parameters to a SSRS report

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.