1

Trying to write a program in which the user inputs their expenses for the week. The trouble comes in the fact that I want the program to re-ask the user if unacceptable input is entered. I figured out how to detect negative values, but when trying to catch an Input Mismatch Exception (if like a character or string is inputted) the loop just runs infinitely asking for "Monday Expenses:" How do I make it so the user is given another chance to answer? I tried a break; but that broke out of the do while loop also, which I don't want.

Here's my code so far:

  import java.util.Scanner;

  public class BarGraph{
  public static void main (String[] args){

Scanner myScanner;
myScanner = new Scanner(System.in);

double mon, tues, wed, thurs, fri, sat, sun;

do{
    try{
        System.out.print("Expenses for Monday: ");
         mon = myScanner.nextDouble();
    }catch( Exception e){
        mon = -1;
    }
}while(mon<0);

System.out.println(mon);

}
}  

Thanks for your help

Clarisa
  • 125
  • 2
  • 12

5 Answers5

2

That call to nextDouble() won't consume a non-double from your Scanner. You need something like,

if (myScanner.hasNextDouble()) {
    mon = myScanner.nextDouble();
} else {
    System.out.printf("%s is not a double.%n", myScanner.next());
}

Also, I'd recommend you extract that logic into a method. Like,

static double getExpensesForDay(Scanner scanner, String day) {
    while (true) {
        System.out.printf("Expenses for %s: ", day);
        System.out.flush();
        if (scanner.hasNextDouble()) {
            return scanner.nextDouble();
        }
        System.out.printf("%s is not a double.%n", scanner.nextLine());
    }
}

Then you could call it like

public static void main(String args[]) {
    Scanner myScanner = new Scanner(System.in);
    double mon = getExpensesForDay(myScanner, "Monday");
    double tues = getExpensesForDay(myScanner, "Tuesday");
    double wed = getExpensesForDay(myScanner, "Wednesday");
    double thurs = getExpensesForDay(myScanner, "Thursday");
    double fri = getExpensesForDay(myScanner, "Friday");
    double sat = getExpensesForDay(myScanner, "Saturday");
    double sun = getExpensesForDay(myScanner, "Sunday");
    // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

It's because it always go to the catch block when the scanner cannot parse a double value. which fits your condition

mon<0

it always get -1 value, it causes infinite loop you might have mistaken your condition into

while(mon>0)
Sheychan
  • 2,415
  • 14
  • 32
1

you can use myScanner.nextLine(); to dump out all unnecessary chars in the cache before beginning the next loop

import java.util.Scanner;

public class BarGraph {

    public static void main(String[] args) {

        Scanner myScanner;
        myScanner = new Scanner(System.in);

        double mon, tues, wed, thurs, fri, sat, sun;

        do {
            try {
                System.out.print("Expenses for Monday: ");
                mon = myScanner.nextDouble();
            } catch (Exception e) {
                mon = -1;
                myScanner.nextLine();
            }
        } while (mon < 0);

        System.out.println(mon);
    }
}
0

Replace

mon = myScanner.nextDouble(); 

with following

mon = Double.parseDouble(myScanner.next());
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
0

You can try below code. It will go through whole array. You can add more validation as you want.

import java.util.Scanner;
public class BarGraph 
{

public static void main(String[] args)
{
    System.out.println("Welcome to Expenses Program: " + "\n");

    week(); 
}

public static void week()
{
    Scanner myScanner;
    myScanner = new Scanner(System.in);
    String [] day = new String[] {"mon", "tues", "wed", "thurs", "fri", "sat", "sun"};

        for (String days : day)
        {
            System.out.println("Expenses for " + days + ":");
            float exp = myScanner.nextFloat();

        }   
}
}
KhiLan PaTeL
  • 123
  • 2
  • 4