2013-03-27 6 views
0

Heres 내 코드는 프로그램에서 수행하려고하는 내용을 요약합니다.이 오류 메시지의 원인은 무엇입니까?

// This program will read in from a file a number that tells how many 
// wages and hours worked the file contain. And then calculates the 
// salary 
import java.util.*; 
import java.text.*; 

public class Lab10 
{ 
     public static void main(String[] args) 
     { 
      // Declare variables 
      Scanner scan = new Scanner(System.in); 
      DecimalFormat fmt = new DecimalFormat("0.00"); 
      String name; 
      double wages; 
      double hoursWorked; 
      double salary; 
      int numCalculate; 
      int EmployeeNumber ; 
      int i = 0 ; 

      // Read in how many employees the file contains 
      EmployeeNumber =scan.nextInt() ; 

      // for loop that reads and records the name hoursworked and wages , and prints out the information 
      for (; i < EmployeeNumber +1; i ++) 
      { 
      name = scan.next() ; 
      wages = scan.nextDouble() ; 
      hoursWorked = scan.nextDouble() ; 

      salary = calculateSalary(wages,hoursWorked) ; 
      System.out.println(name +" worked " +fmt.format(hoursWorked) + " with a wage of " + "$" +fmt.format(wages) + " and got paid $" +fmt.format(salary)); 
      } 
      // End of control loop 

     } // End of main method 

     public static double calculateSalary(double wages, double hoursWorked) 
     { 
//Declare constants 
      final int OVERTIME_BREAK = 40; 
      final double OVERTIME_MULTIPLE = 1.5; 
      double salary = 0 ; 

      //Calculate salary 

      if(hoursWorked > OVERTIME_BREAK) 
      { 
      salary = (hoursWorked - OVERTIME_BREAK) * wages * OVERTIME_MULTIPLE + hoursWorked * wages; 
      } 
      else 
      { 
      salary = hoursWorked * wages; 
      } 
      // Return the salary 
      return salary ; 

     } // end of calculateSalary method 
} // End of Lab10 class 

이 입력 파일 사용 : 내 원하는 출력을 얻을

3 
Smith 12.50 25 
Jones 25.89 60 
Brown 7.86 19.89 

하지만 결국에 압정으로 고정이 메시지 :

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:855) 
    at java.util.Scanner.next(Scanner.java:1364) 
    at Lab10.main(Lab10.java:32) 

사람이 무엇을 의미하는지 말해 줄래 방법을 그것을 해결하기 위해?

답변

2

너무 많이 반복하고 있습니다.

당신은 3 명 직원이이 같은 단지 루프 그에해야합니다

for (; i < EmployeeNumber; i ++) 
{ 
    name = scan.next() ; 
    ... 
} 
+0

감사합니다. 이제 작동합니다. –

0

자바의 배열 인덱스는 0으로 시작을, 그래서 당신이 3 개 요소가있는 경우 그 0, 1에 저장되고, 2로 말했다 위의 루프에 위의 코드를 입력하면 < EmplyeeNumber는 충분하지만 EmployeeNumber +1이 아닙니다.

관련 문제