] import java.util.*; public class BiddingOnItems{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); String s = getBiddingPrice(scan); String items = getNumberOfProducts(scan); System.out.println("you chose " + s + " for your price"); System.out.println("you chose " + items + " for your quantity"); } public static String getBiddingPrice(Scanner scan){ System.out.print("What is your bidding price (ex. 8.50):"); String s = scan.nextLine(); return s; } public static String getNumberOfProducts(Scanner scan){ System.out.print("How mant items are you wanting to purchase: "); String items = scan.nextLine(); try{ int value = Integer.parseInt(items); return items; } catch(NumberFormatException e){ System.out.println("Error in input, please enter a valid quantity"); getNumberOfProducts(scan); return items; } } }
I'm having some trouble with my code, I am asking the user to input two things, the amount of money to bid on an item, and the quantity of the items they are wanting to bid on.
The program is suppose to go through and check if the input is valid, as in the amount of money must be numerical but can have one decimal (ex. 8.50). And the quantity must be checked to assure that it is an integer since you can't bid on 1.5 items.
I may end up asking more questions later on based on this code, but right now, I am having trouble with the output it gives me for the quantity of items.
I purposely put in 2.5 for the quantity, and it successfully catches the error and asks me to reenter a valid input. So 3 is entered which is valid, so it goes through without catching a problem, but when the output is given, 2.5 is returned and not 3. How do I make it so I update the value? Cause it seems I have coded wrong that it is keeping the first value entered and not updating it with the correct valid input.