0

This is the output I want to get: Enter an ID: 456 Brad worked 36.80 hours (7.36 hours/day)

Do you want to search again? y

Enter an ID: 293 ID #293 not found

Do you want to search again? y

Enter an ID: Kim ID is not valid

Do you want to search again? nope

Have a nice day!

And this is a part I stuck because I can't get the quote " ID # was not found." and " The ID is not valid." And this is what message I received: Exception in thread "main" java. util.NoSuchElementException

public class HoursAssign {

    public static void main (String [] args) throws FileNotFoundException {
        Scanner console = new Scanner (System.in);
        Scanner input = new Scanner (new File ("hours.txt"));
        String answer; int ID; 
        do{
            System.out.print ("Enter an ID: ");
            int searchID = console.nextInt ();
            String line = findPerson ( input, searchID);
            if  (line.length() > 0)
                processLine ( line);
            else if (!console.hasNextInt()){
                System.out.print ("The ID is not valid.");
            }
            console.nextLine();
            System.out.print ("\n Do you want to search again ? ");
            answer = console.nextLine();
            answer = answer.toLowerCase();
        } while (answer.charAt (0) != 'n');
        System.out.print (" Have a good day!");
    }

    public static String findPerson (Scanner input, int searchID){
        while (input.hasNextLine()) {
            String line = input.nextLine();
            Scanner newLine = new Scanner (line);
            int ID = newLine.nextInt();
            if (ID == searchID){
                return line;
            } 
        }
        System.out.print (" The ID #" + searchID + " not found.");
        return " ";
    }
}
 
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346

0 Answers0