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 7821663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:43:13+00:00 2026-06-02T07:43:13+00:00

My code is supposed to read input from a .txt file, and then do

  • 0

My code is supposed to read input from a .txt file, and then do different things to sort it. The first int is the length. My main problem is I can not figure out why I keep getting a mismatch error, or if i put in ‘junk’ statements, the program never finishes. I’ll post the .txt first, and then the program.

15
Smith, John
26
Baker
Jones, Susan
15
Student
Mouse, Mickey
31
Theme park employee
Mouse, Mighty
48
Cartoon super hero
Anderson, William
35
Computer Programmer
Parker, Cindy
18
Author
McCain, John
20
Student
Armstrong, Michelle
17
Student
Thompson, Anne
29
Doctor
Li, Steve
15
Student
James, Tanya
20
Student
Moore, James
32
Teacher
Andrews, Julie
75
Actress
Obama, Michelle
46
Lawyer
Michaels, Todd
51
Student

//Don’t forget to copy the blank line at the end.

program starts here.

import java.util.Scanner;
import java.io.*;
public class SortAndDisplayCustomerData
{
public int length; //The length of the names, ages, and occupations arrays
public String[] names;
public int[] ages;
public String[] occupations;
public int count; //The length of the studentNames and studentAges arrays
public String[] studentNames;
public int[] studentAges;
public int i, minPos, Temp2, y, minVal;
public String Temp, Temp3, temp2, minVal2;

public void getDataFromFile()
{
Scanner keyboard = new Scanner(System.in);
    Scanner inputStream = null;
System.out.println("wtf");
    try
    {
        inputStream = new Scanner(new FileInputStream("NameAgeOcc.txt"));
    }

    catch(FileNotFoundException error)
    {
        System.out.println("Unable to open input file.");
        System.exit(0);
    }
    System.out.println("wtf2");
    length=inputStream.nextInt();
    //String junk = keyboard.nextLine();
    System.out.println("wtf3");
    names = new String[length];
    ages = new int[length];
    occupations = new String[length];
    for(i=0;i<length;i++)
    {
        names[i]=inputStream.nextLine();
        ages[i]=inputStream.nextInt();
        occupations[i]=inputStream.nextLine();
    }
    inputStream.close();
} 

public void displayAllFileData()
{
System.out.println("wtf3");
System.out.printf("%-25s%-8s%24s%n","Names","  Ages","  Occupations");
    for(i=0;i<length;i++)
    {
        System.out.printf("%-25s%6d%-24s%n",names[i],ages[i],occupations[i]);
    }
}
public void sortAllDataByAge()
{
    for(i=0;i<length;i++)
    {
        minVal=ages[i];
        minPos=i;
        for(y=i+1;y<length;y++)
        {
            if(ages[y]<minVal)
            {
                minVal=ages[y];
                minPos=y;
            }
        }
        Temp2 = ages[minPos];
        ages[minPos] = ages[i];
        ages[i] = Temp2;
        Temp = names[minPos];
        names[minPos] = names[i];
        names[i] = Temp;
        Temp3 = occupations[minPos];
        occupations[minPos] = occupations[i];
        occupations[i] = Temp3;
    }
}
public void extractStudentData()
{
    count=0;
    for (i=0;i<length;i++)
    {
        if(occupations[i].equalsIgnoreCase("student"))
        count++;
    }
    int j=0;
    studentAges = new int[count];
    studentNames = new String[count];
    for (i=0;i<length;i++)
    {
        if(occupations[i].equalsIgnoreCase("student"))
        {
            studentAges[j]=ages[i];
            studentNames[j]=names[i];
            j++;
        }
        }
}
public void displayStudentData()
{ 
System.out.printf("%n%-25s%-8s%n","Names","  Ages");

    for (i=0;i<count;i++)
    {
        System.out.printf("%-25s%6d%n",studentNames[i],studentAges[i]);
    }

}
public void sortStudentDataAlpha()
{ 
    for(i=0;i<count;i++)
    {
        minVal2=studentNames[i];
        minPos=i;
        for(y=i+1;y<count;y++)
        {
            if(studentNames[y].compareToIgnoreCase(minVal2)<0)
            {
                minVal2=studentNames[y];
                minPos=y;
            }
        }
        Temp = studentNames[minPos];
        studentNames[minPos] = studentNames[i];
        studentNames[i] = Temp;
        Temp2 = studentAges[minPos];
        studentAges[minPos] = studentAges[i];
        studentAges[i] = Temp2;
    }
}

}
  • 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-02T07:43:14+00:00Added an answer on June 2, 2026 at 7:43 am

    Following a nextInt() you should eat up the line-break char, otherwise you’ll be expecting a String (for the names in your text file) and you’ll get an empty string (line break). Then you’ll have a nextInt() again but the pointer in the text file will be on the name (String).

    For example, your program is following a path like this:

    32 
    Teacher 
    Andrews, Julie 
    75 
    Actress 
    Obama, Michelle 
    

    nextInt() -> 32 nextLine() -> nextLine() -> Teacher
    nextInt() -> Andrews, [Type Mismatch]

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

Sidebar

Related Questions

This code is supposed to read postfix problems from a file and and write
My code is supposed to read a line from the user, if the line
I'm trying to use basic Java code in Scala to read from a file
I am supposed to write a program that should read from input numbers in
The following bit of SAS code is supposed to read from a dataset which
This code is supposed to be able to sort the items in self.array based
Given the following code (it's supposed to write helloworld in a helloworld file, and
I need to create a SAS dataset from a binary file. My code is:
I want to first form a 3-digit code once a user enters some input
I have written a piece of a code which is supposed to read the

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.