Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing String Arrays Outputs Null
    primarykey
    data
    text
    <p>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 </p> <pre><code>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&lt;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&lt;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&lt;length;i++) { minVal=ages[i]; minPos=i; for(y=i+1;y&lt;length;y++) { if(ages[y]&lt;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&lt;length;i++) { System.out.printf("%-25s%6d%24s%n",names[i],ages[i],occupations[i]); } int studentCount=0; for (i=0;i&lt;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&lt;studentCount;i++) { System.out.printf("%-25s%6d%n",studentNames[i],studentAges[i]); } inputStream.close(); } } </code></pre> <p>The .txt</p> <pre><code>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 </code></pre> <p><em>Make sure you copy the empty line at the end of the .txt (hit enter or return)</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload