简体   繁体   中英

Comparing String Arrays Outputs Null

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)

I've check your code briefly and I've found this:

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

";" at the end of "if" causes that studentCount is always incremented.

Of course there might be more mistakes in your code. If you want people to help you, you should explain what your program is doing.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM