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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:18:07+00:00 2026-06-01T01:18:07+00:00

The program outputs a bunch of nulls when comparing. I think I am not

  • 0

The program outputs a bunch of nulls when comparing. I think I am not comparing correctly, although I have tried == and that outputs nothing. Below is the code and below that is the NameAgeOcc.txt

import java.util.Scanner;
import java.io.*;
public class MoreSelectionSortAndParallelArrays
{
public static void main(String[] args)
{
    Scanner inputStream = null;

    try
    {
        inputStream = new Scanner(new FileInputStream("NameAgeOcc.txt"));
    }

    catch(FileNotFoundException error)
    {
        System.out.println("Unable to open input file.");
        System.exit(0);
    }

    int length=inputStream.nextInt();
    int ages [] = new int[length];
    String names [] = new String[length];
    String occupations [] = new String[length];
    String junk= inputStream.nextLine() ;
    int i;
    for(i=0;i<length;i++)
    {
        names[i]=inputStream.nextLine();
        ages[i]=inputStream.nextInt();
        junk=inputStream.nextLine();
        occupations[i]=inputStream.nextLine();
    }
    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]);
    }
    String Temp, Temp3;
    int minVal,minPos,y,Temp2;
    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;
    }
    System.out.println();
    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]);
    }
    int studentCount=0;
    for (i=0;i<length;i++)
    {
        if(occupations[i].equalsIgnoreCase("student"));
        studentCount++;
    }
    int studentAges [] = new int[studentCount];
    String studentNames [] = new String[studentCount];
    System.out.printf("%-25s%-8s%n","Names","  Ages");

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

The .txt

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

Make sure you copy the empty line at the end of the .txt (hit enter or return)

  • 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-01T01:18:08+00:00Added an answer on June 1, 2026 at 1:18 am

    Program outputs bunch of nulls because you are creating studentAges and studentNames, but you are not putting something into it.

    @Paweł Adamski’s answer is incorrect

    if(occupations[i].equalsIgnoreCase("student"));
    studentCount++;
    

    means that you are increasing studentCount always.

    I would recommend you to use some inner class and than use Arrays.sort() for sorting (You have to implement Comparable interface or implements Comparator interface for sorting).

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

Sidebar

Related Questions

I have a little c# console program that outputs some text using Console.WriteLine. I
I have a Perl program that reads in a bunch of data, munges it,
Here is a special Haskell program which outputs a Python program that outputs a
A while ago, I wrote a program that outputs any localhost or network database
I wrote a program that accepts and outputs Hebrew (i.e. right-to-left) text. In lieu
I have a command line program, which outputs logging to the screen. I want
For about a year I have been thinking about writing a program that writes
I have to write a program that performs highly computationally intensive calculations. The program
Specifically, if I use NInject to create a bunch of objects that have been
So I have a directory of with a bunch of sub-directories that contain .java

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.