2013-10-24 52 views
0
/*This assignment is to create 2 parallel arrays – jobs& salaries 
*Read file from 「careers.txt」 into arrays 
*one line – job 
*next line –salary 
*Sort Salaries {from highest} 
*then swap jobs if salary swapped 
*Display output of careers and salaries from the highest on a formatted table. 
* 
*/ 
package assignment7; 
import java.util.*;//importing Scanner 


public class Coordinator 
{ 


    public static void main(String[] args) throws Exception 
    {// the main method creates the arrays and calls on each method to perform 
     //its work. 
    String [] job = new String[20];//creating a string array having 20 spaces 
    int[] salary = new int[20];//creating an integer array having 20 spaces 
    int count;// number of spaces actually occupied in the array 

    **count = readFile(job, salary);**// calling a method to read text into both 
            //arrays and return the number of spaces 
            //occupied in the array 

    sorter(job,salary,count);// calling on method to arrange file from highest 
    //to lowest 

    display(job, salary,count);// calling on method present the output 


    } 
    public static int readFile(String[] jobber, int[] salaro) throws Exception 
    { // this method reads a text file and copies into arrays and also 
     //returns the number of spaces occupied in the array 
      int n = 0; //keeps track of number of times a line is fed into an 
      //array 

      //set up a file class object linked up to the name of the file to 
      //be read 
      java.io.File unread = new java.io.File("career.txt"); 

      // create a scanner instance to read the input from the file 
      Scanner infile = new Scanner(unread); 

      /*This while loop reads line of text into the arrays, it uses 
      * boolean 
      * function hasNextLine() and the created scanner instance. 
      */ 
      while (infile.hasNextLine() || infile.hasNextInt()) 
      { 
       jobber[n] = infile.nextLine(); 
       **salaro[n] = infile.nextInt()**; 
       n++; 
      }//end while 
      infile.close();//close scanner class 
      return n;// return number of spaces filled 

    }//end of readFile method 


    public static void sorter(String[] jobestic, int[] salawe, int z) 
      throws Exception 
    {// this method sorts the array from the highest paid job to the lowest. 

     boolean swapped;// keeps track of when a swap takes place 
     int i;// variable fo for loop 
     int temp;// helps in swap 
     String temp2;// helps in swap 

     do 
     { 
      swapped = false; 
      for (i= 0; i < z-1; i++)// a pass through the array 
      { 
       if (salawe[i+1] > salawe[i]) 
       // if the number before it is less they swap 
       { 
        //swap starts 
        temp = salawe[i+1]; 
        salawe[i+1] = salawe[i]; 
        salawe[i] = temp; 
        //swaps the jobs too if the salary is swapped 
        temp2 = jobestic[i+1]; 
        jobestic[i+1] = jobestic[i]; 
        jobestic[i] = temp2; 

        swapped = true; 
       }// end if 


      }// end for 

     } while (swapped); 
    }// end sorter method 

    public static void display(String[] jobo, int[] salary5 ,int k) throws Exception 
    { 

     //this method displays the output as a formatted table 
     int i; 
     System.out.printf("%-60s%15s%n", "Job", "Salary"); 





     for(i=0; i<k; i++) 



      System.out.printf("%-60s%,15d%n", jobo[i], salary5[i]); 

    } 








} 

請老兄,這段代碼有什麼問題?我一遍又一遍地思索着我的大腦,無法弄清楚什麼是錯誤的......我運行了這段代碼,但是我一直在收到錯誤...錯誤總是表明粗體行。我正在嘗試閱讀的文本文件如下。我想將工作職位存儲在一個數組中,並將相應的工資存儲在另一個數組中。像並行陣列一樣。請問代碼有什麼問題?我想將工資存儲爲整數,因爲我在呈現輸出時將其格式化爲整數。 謝謝。從java中的文本文件中讀取字符串和整數

Computer and Information Research Scientists 
102190 
Computer and Information Analysts 
80460 
Computer Systems Analysts 
79680 
Information Security Analysts 
86170 
Software Developers and Programmers 
87100 
Computer Programmers 
74280 
Software Developers(Applications) 
90060 
Software Developers(Systems Software) 
99000 
Web Developers 
62500 
Database and Systems Administrators and Network Architects 
76880 
Database Administrators 
77080 
Network and Computer Systems Administrators 
72560 
Computer Network Architects 
91000 
Computer Support Specialists 
48900 
Computer User Support Specialists 
46420 
Computer Network Support Specialists 
59090 

錯誤:

Exception in thread "main" java.util.InputMismatchException 
at java.util.Scanner.throwFor(Scanner.java:909) 
at java.util.Scanner.next(Scanner.java:1530) 
at java.util.Scanner.nextInt(Scanner.java:2160) 
at java.util.Scanner.nextInt(Scanner.java:2119) 
at assignment7.Coordinator.readFile(Coordinator.java:56) 
at assignment7.Coordinator.main(Coordinator.java:25) 
+0

你會得到什麼錯誤? –

+1

停下。放下所有這些代碼。開始一個新項目,重點放在文件IO上。儘可能簡化代碼。試着弄清楚如何做文件IO。如果你在這個簡短的小項目中遇到了問題,那就把它帶到這裏吧,所以我們不需要閱讀大量代碼,這些代碼與你看起來沒有任何問題無關。 – nhgrif

+0

@nhgrif我明白你的意思,但是這段代碼是用來練習File IO的,而且我似乎在某個地方有問題。請幫助我通過它。謝謝。 – user2913790

回答

0

這個問題,或有同等作用,似乎有人問每隔幾天。

當您撥打nextInt()時,掃描儀會在號碼後停止。然後,當您立即致電nextLine()時,您實際上正在讀取具有該號碼的行末處的換行符。你不讀下一行。因此,在循環的下一次迭代中,如果在掃描儀中排列了非數字文本,則會調用nextInt()

在每次致電nextInt()後再增加一個致電nextLine()的電話,以便閱讀該額外的換行符。