Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having trouble returning the right out put.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble returning the right out put.

    ]
    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.
    Last edited by helloworld922; March 27th, 2011 at 12:43 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble returning the right out put.

    catch(NumberFormatException e){
    			System.out.println("Error in input, please enter a valid quantity");
    			getNumberOfProducts(scan); // recursive call right here
    			return items;
    		}
    You have a very subtle recursive call in your exception handler. In Java every time you call a method you get a new local scope and just because the local variable names happen to line up does not mean that changing the value of one will do anything to the value of the other one. In fact, it's using totally different local variables which just happen to have the same name.

    Instead of using a recursive call in this manner try using a loop. Or for the quick and dirty solution, try return the value that recursive call gives. Either solution works, but the first solution is a better solution.

Similar Threads

  1. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  2. If statement not returning anything
    By Jonathan_C in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2010, 03:38 PM
  3. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 01:53 PM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 01:00 AM