0

The code is in my first method, payCalc();. my NoSuchElementException is catching every time i run it. The program will prompt the menu, and if you choose case 1 it will prompt for the names, then loop back to the menu. I've tried removing various things or commenting things out, but i'm just not sure what is causing the problem. This is my first program working with try/catch and throws so possibly it is a misunderstanding of the workings on my part.

import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class SemesterProject_PartC
{
    public static void main(String[] args) throws FileNotFoundException 
    {
    Scanner in = new Scanner(System.in);  
try
{
    int menuOption, quitCheck;//menu variable

do{ //menu is repeated after a case is selected and completed
    System.out.println("Calculator Menu");
    System.out.println("1) Wage Calculator");
    System.out.println("2) Grocery Coupon Calculator");
    System.out.println("3) Quit");
    System.out.print("Which Calculator would you like to use? ");
    menuOption = in.nextInt();

    switch (menuOption)
    {
    case 1:
        payCalc();  //wage calculator
    break;

    case 2:
        groceryCalc(LENGTH, discount, discount);//grocery calculator
    break;

    case 3: 
         //future method goes here
    break;
    case 4:
        payCalc_Reader(null);
    break;
    case 5:  //'quit' case\
        System.out.println("Are you sure you want to quit, enter 1 for yes or 2 for no");
        quitCheck = in.nextInt();
        if (quitCheck == 1)
            System.out.println("Thank you for using this program. Have a nice day!");
        else
            menuOption = 10;
    break;
    /*case 4:
        read in the file
        System.out.print(count);
    break; */

    default: //in case of invalid input
            System.out.println();
        System.out.println("INVALID INPUT");
        System.out.println("Input must be '1' for wage calculator, '2' for grocery calculator, or '3' to quit");
            System.out.println(); //leaves an empty space between default case display and menu prompt

    }//switch
   }//end do
while (menuOption != 5); //stops menu loop and terminates program if case 3 is selected
}
catch (NoSuchElementException ex)
{
    System.out.println("Sorry, you may only enter numbers");
    ex.printStackTrace();
}
        }//main

/**Method payCalc - calculates regular hour paycheck / calls overtime method if needed
 *@param  none
 *@return none      pay calculations are not used again in the main, no need for return
 * @throws FileNotFoundException 
 */
public static void payCalc() throws FileNotFoundException
{
try{
File outputFile = new File ("payCalc.txt");
PrintWriter out = new PrintWriter (outputFile);
Scanner in = new Scanner(new File ("payCalc.txt"));

double hourlyWage, regularPay, hoursWorked, overtimeWorked = 0; //payCalc variables
String firstName, lastName;

    System.out.println("Please enter your first and last name: ");
        firstName = in.next();
        lastName = in.next();

do{ //catches negative inputs and re-prompts
    System.out.println("Please enter your hourly wage");
    System.out.print("$");
        hourlyWage = in.nextDouble();
    if (hourlyWage < 0)
        System.out.println("It may feel like that sometimes, but please only use postivie numbers");
  }//end do
while (hourlyWage < 0);

do{
    System.out.println("Please enter your regular paid hours worked during the week: ");
        hoursWorked = in.nextDouble();
    if (hoursWorked < 0)
        System.out.println("Sorry, please only use positive numbers"); //catches negative inputs and re-prompts
    if (hoursWorked > 40) 
    { //catches inputs above 40 and re-prompts
    System.out.println("Sorry, please only enter your regular paid hours worked (0-40),");
    System.out.println("If you have overtime hours you will be asked for them shortly");
        System.out.println(); //regular hours and overtime hours are prompted separately
    }
   }//end do
while (hoursWorked < 0 || hoursWorked > 40); //valid inputs

regularPay = hourlyWage * hoursWorked;//regular pay calculations

System.out.printf("Hello %s, ", firstName);//greeting is always displayed
    System.out.println();

if (hoursWorked == 40) //asks for overtime hours if user worked 40 regular hours
{
System.out.println("Please enter any overtime hours you worked");
System.out.println("If you didn't work any, you may just enter '0'");
overtimeWorked = in.nextDouble();
}
    if (overtimeWorked > 0) //if user worked overtime call overtimeCalc method
        overtimeCalc(hoursWorked, hourlyWage, regularPay, overtimeWorked);
    else
    { //if user entered '0', do regular pay calculations with no overtime

        out.printf("Wage: %s %s, Hourly Pay: $%.2f, Regular Hours: %.2f, Gross Pay: $%.2f", firstName, lastName, hourlyWage, hoursWorked, regularPay);
        String message = in.nextLine();
        payCalc_Reader(message);


    }
     System.out.println();
System.out.printf("Thank you for using this Wage Calculator %s %s, Have a great day!", firstName, lastName);
    System.out.println(); //closing statement is always given after wage calculations are given
}
catch (NoSuchElementException ex)
{
    System.out.println("Sorry, you may only enter numbers");
    ex.printStackTrace();
} 
}//end payCalc

//other methods and junk go here.
    }//class
FoxXg
  • 1
  • 3
  • Please reduce your code down to a [mcve] that demonstrates your problem. Also, take a bit of time to format your code, it's extremely difficult to read as-is. – azurefrog Apr 24 '17 at 21:11
  • Please post the stacktrace – opensam Apr 24 '17 at 21:11
  • Take a look at [How to handle infinite loop caused by invalid input using Scanner](http://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-using-scanner) – Pshemo Apr 24 '17 at 21:15
  • Thank you for that link, but it seems like it is detailing what happens when you enter in a wrong data type. Even when i enter an integer it still catches it. – FoxXg Apr 24 '17 at 21:23

0 Answers0