2012-03-30 103 views
1

比較時,程序輸出一串空值。我認爲我沒有正確比較,儘管我已經嘗試過==並且不輸出任何內容。下面是代碼再下面是NameAgeOcc.txt比較字符串數組輸出Null

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(); 
} 
} 

中的.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 

確保複製的空行的.TXT結束(按回車鍵或返回)

回答

0

程序輸出一串空值,因爲您正在創建studentAgesstudentNames,但是您並未在其中添加任何內容。

@帕維爾亞當斯基的回答是不正確

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

意味着你總是增加studentCount

我建議您使用一些內部類,並使用Arrays.sort()進行排序(您必須實現Comparable接口或實現Comparator接口進行排序)。

+0

謝謝。我現在覺得有點笨,但我得到它的工作! – Chromey 2012-03-30 07:50:15

0

我已經檢查你的代碼簡單,我已經發現這一點:

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

「;」在「if」結尾導致studentCount總是遞增。

當然,你的代碼中可能會有更多的錯誤。如果你想讓別人幫你,你應該解釋你的程序在做什麼。

+0

好的,我的程序(至少是我有問題的部分)試圖計算職業數組中的學生,然後創建新數組,studentNames和studentAges,然後按字母順序組織studentNames,同時保留studentAges完整。 – Chromey 2012-03-30 07:37:58

+0

我根據@Betlista的帖子糾正了我的答案。 – 2012-03-30 07:51:10