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 10 of 10

Thread: need help with homework program...thanks

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default need help with homework program...thanks

    ok im in an entry level class and very new to java so your help would be greatly appreciated..here is what were supposed to do...

    Write a program to take bids for an auction business. The program prompts the user to enter the name of item desired, next prompts the user to enter the bid price, next prompt the user to enter the desired method of shipping. There are three shipping methods available. ( 0 means ground shipping four day delivery, 1 means ground express two day delivery, 2 means air shipping one day delivery ). No charge for ground four day delivery, there is a $5.00 charge for ground express and $15.00 charge for air shipping ). Assume that the user of the program will be perfectly alert and a perfect typist, will not make any mental or typing mistakes.

    A sample run would look like this :

    Enter Item you are bidding on : bicycle
    Enter your bid price : 50.00
    Enter desired delivery method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) : 1

    Invoice :
    Item : Bicycle
    Your bid price : 50.00
    shipping : 5.00
    total : 55.00

    here is my program so far...

     
    import java.util.Scanner;
     
    public class Assign4_Roberts{
    	public static void main(String[] args){
     
    		//enter shipping prices
    		double ground = 0;
    		double express = 5;
    		double air = 15;
     
    		//create a scanner
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the item you are bidding on :");
    		int answer =input.nextInt(); 
     
    		System.out.println("Enter your maximum bid: ");
    		int num = input.nextInt();
     
     
    		System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    		int shipping = input.nextInt();
     
    		if(shipping == 0)
    			System.out.println("total:" + num + ground);
     
    		if(shipping == 1)
    			System.out.println("total:" + num + express);
     
    		if(shipping == 2)
    			System.out.println("total:" + num + air);
     
    		//invoice
    		System.out.println("Invoice :");
    		System.out.println("Item :" + answer);
    		System.out.println("Your Bid Price :" + num);
    		System.out.println("Total :" + num + shipping);
     
     
    	}
    }[B][/B]


    and here is what happens when i try to run the program...im using eclipse btw

    Enter the item you are bidding on :
    bicycle
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Assign4_Roberts.main(Assign4_Roberts.java:20)


    anybody know what im doing wrong
    Last edited by robertsbd; October 3rd, 2010 at 09:59 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: need help with homework program...thanks

    Read my signature to get faster and better help. I'm not an Eclipse user, but that probably isn't the cause of this particular issue. You have followed guidelines #2 and #3, but fix #1 on your current and future posts. While you add code tags, I'll read your code to the best I can.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: need help with homework program...thanks

    Ok, I found the problem.

    Time for a quick JAVA Basic Types Review:
    In Java, there a 3 primary basic types (and a bunch of other ones for other things). Basic Type 1 is int. int is short of Integer. An integer is any whole number. Basic Type 2 is a double. A double is any number with decimal places. Basic Type 3 is String. A String is a word or phrase.

    (Sorry for all the bolding but I wanted to highlight the important parts)

    Now, this error:
    at java.util.Scanner.nextInt(Unknown Source)
    Is where the problem exists. But why is that a problem? Because you are inputting a number with decimal places. The method nextInt searches for the next integer, but it doesn't find one. Instead, you should be searching for the next double.

    I would usually go more in-depth, but I don't know where to go from here. Tell me how much of this you understand.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: need help with homework program...thanks

    i thought I did but apparently not because heres what i tried to change and got the same thing

     
     
    import java.util.Scanner;
     
    public class Assign4_Roberts{
    	public static void main(String[] args){
     
    		//enter shipping prices
    		double ground = 0.00;
    		double express = 5.00;
    		double air = 15.00;
     
    		//create a scanner
    		Scanner in = new Scanner(System.in);
     
    		System.out.println("Enter the item you are bidding on :");
    		double answer =in.nextDouble(); 
     
    		System.out.println("Enter your maximum bid: ");
    		double num = in.nextDouble();
     
     
    		System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    		double shipping = in.nextDouble();
     
    		if(shipping == 0)
    			System.out.println("total:" + num + ground);
     
    		if(shipping == 1)
    			System.out.println("total:" + num + express);
     
    		if(shipping == 2)
    			System.out.println("total:" + num + air);
     
    		//invoice
    		System.out.println("Invoice :");
    		System.out.println("Item :" + answer);
    		System.out.println("Your Bid Price :" + num);
    		System.out.println("Total :" + num + shipping);
     
     
    	}
    }[B][/B]

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: need help with homework program...thanks

    You have to deal with each input uniquely.

    When you ask:
    System.out.println("Enter the item you are bidding on :");
    What basic type are you expecting to receive? Since you are expecting something like:
    bicycle
    you should be trying to get a String, not a double or an int.

    When you ask:
    System.out.println("Enter your maximum bid: ");
    What basic type are you expecting to receive? Since you are expecting something like:
    50.00
    you should be trying to get a double.

    When you ask:
    System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    What basic type are you expecting to receive? Since you are expecting something like:
    1
    you should be trying to get an int.

    Tell me if you understand what I am saying. If you do understand, see if you can fix your code accordingly. I can tell you understand the basics of Scanner, but here is a recap of the method options for what you are attempting:
    To get a String:
    Scanner.nextLine() OR Scanner.next()
    To get a double:
    Scanner.nextDouble();
    To get an int:
    Scanner.nextInt();

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with homework program...thanks

    Quote Originally Posted by robertsbd View Post
    ok im in an entry level class and very new to java so your help would be greatly appreciated..here is what were supposed to do...

    Write a program to take bids for an auction business. The program prompts the user to enter the name of item desired, next prompts the user to enter the bid price, next prompt the user to enter the desired method of shipping. There are three shipping methods available. ( 0 means ground shipping four day delivery, 1 means ground express two day delivery, 2 means air shipping one day delivery ). No charge for ground four day delivery, there is a $5.00 charge for ground express and $15.00 charge for air shipping ). Assume that the user of the program will be perfectly alert and a perfect typist, will not make any mental or typing mistakes.

    A sample run would look like this :

    Enter Item you are bidding on : bicycle
    Enter your bid price : 50.00
    Enter desired delivery method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) : 1

    Invoice :
    Item : Bicycle
    Your bid price : 50.00
    shipping : 5.00
    total : 55.00

    here is my program so far...

     
    import java.util.Scanner;
     
    public class Assign4_Roberts{
    	public static void main(String[] args){
     
    		//enter shipping prices
    		double ground = 0;
    		double express = 5;
    		double air = 15;
     
    		//create a scanner
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the item you are bidding on :");
    		int answer =input.nextInt(); 
     
    		System.out.println("Enter your maximum bid: ");
    		int num = input.nextInt();
     
     
    		System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    		int shipping = input.nextInt();
     
    		if(shipping == 0)
    			System.out.println("total:" + num + ground);
     
    		if(shipping == 1)
    			System.out.println("total:" + num + express);
     
    		if(shipping == 2)
    			System.out.println("total:" + num + air);
     
    		//invoice
    		System.out.println("Invoice :");
    		System.out.println("Item :" + answer);
    		System.out.println("Your Bid Price :" + num);
    		System.out.println("Total :" + num + shipping);
     
     
    	}
    }[B][/B]


    and here is what happens when i try to run the program...im using eclipse btw

    Enter the item you are bidding on :
    bicycle
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Assign4_Roberts.main(Assign4_Roberts.java:20)


    anybody know what im doing wrong


    Input Mismatch Exception means you're entering the wrong type. It could be the user's fault, but in this case, it's not. You're want them to enter a String but the Scanner is scanning for an integer.

    wouldn't bicycle be a String

    as in

    String answer = input.next();

    I found another Input Mismatch Exception with num.

    Also, shouldn't it be like this:

    System.out.println("Enter your maximum bid: ");
    		double num = input.nextDouble();

    price has decimal points. An int doesn't.

    Also, I've noticed another potential problem.

    System.out.println("Enter your maximum bid: ");
    		double num = input.nextDouble();

    user enters -1000.00 since they want you to give them $1000.00.

    That would make their total cost negative, and you end up giving out cash instead of receiving it.

    Also, is shipping a constant, and if so, is it the same value no matter what the value of shipping is?

    Oh, I see now, shipping is entered, but your println doesn't have:

    System.out.println("Shipping:  $" + shipping);

    Well, actually, you should have your if statements set shipping to the value of whatever shipping method the user chose.

    Also, though you may not need to, should you have something in case they enter an int value outside the range?

    Also, it could look better this way:


    // near beginning of program, or wherever it won't throw a Null Pointer Exception, put this:
     
    double total = 0.00; // creates a double and initializes it to 0.00.  
    double shipping = 0.00; // this creates shipping, and initializes it to 0.00.  Creating it inside the while loop will cause the value in it to be lost after the loop ends, and causing your Invoice to throw a Null Pointer Exception.  
     
    and then later 
    boolean isValidShippingMethod = false; // creates a boolean that checks to see if the int value that the user enter is a match.  If not, it will ask them till they enter a valid one.  (Note, does not stop it if the user causes an Input Mismatch Exception.  
     
    while (isAValidShippingMethod == false)
    {
    	System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    		shipping = input.nextInt();
    if (shipping == 0)
    			{
    shipping = ground;
    total = num + ground;
    System.out.println("total:" + total);
    isAValidShippingMethod = true;  // this will stop the loop and not ask for a value again.  
    		}
    		else if (shipping == 1)
    {
    shipping = express;  
    total = num + express;
    			System.out.println("total:" + total);
    isAValidShippingMethod = true;  // this will stop the loop and not ask for a value again.  
     
    }
     
    		else if (shipping == 2)
    {
    shipping = air;
    total = num + air;
    			System.out.println("total:" + total);
    isAValidShippingMethod = true;  // this will stop the loop and not ask for a value again.  
    }
     
    else
    {
    System.out.println("Enter a valid shipping method.");
    isAValidShippingMethod = false; // this will keep the while loop going till they enter a valid value
    }
     
    }  // end while

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: need help with homework program...thanks

    Quote Originally Posted by aussiemcgr View Post
    You have to deal with each input uniquely.

    When you ask:
    System.out.println("Enter the item you are bidding on :");
    What basic type are you expecting to receive? Since you are expecting something like:

    you should be trying to get a String, not a double or an int.

    When you ask:
    System.out.println("Enter your maximum bid: ");
    What basic type are you expecting to receive? Since you are expecting something like:

    you should be trying to get a double.

    When you ask:
    System.out.println("Enter  desired  delivery  method ( 0 for ground four day, 1 for ground express, 2 for air shipping ) :");
    What basic type are you expecting to receive? Since you are expecting something like:

    you should be trying to get an int.

    Tell me if you understand what I am saying. If you do understand, see if you can fix your code accordingly. I can tell you understand the basics of Scanner, but here is a recap of the method options for what you are attempting:
    To get a String:
    Scanner.nextLine() OR Scanner.next()
    To get a double:
    Scanner.nextDouble();
    To get an int:
    Scanner.nextInt();
    Actually, it's the scanner reference variable I think he's using, not the class.

    Also, something has been confusing me for a long time:

    Is it valid to have

    Scanner console = new Scanner(System.in);

    in the main method, or does it have to be:

    static Scanner console = new Scanner(System.in);

    ?

    Won't it say:

    non-static variable/method cannot be referenced from a static context if you do the first one, as the main method is static, and I thought a static method could only call other static methods. Maybe that's only the case for it's own static methods, not the static or non-static methods that you use another class to call.

    Also, why does main have to be like this:

    public static void main(String[] args)

    ?

    Why an array of Strings? Why not a Vector or an ArrayList of Strings?

    Also, what's to stop the programmer from creating two main methods?

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: need help with homework program...thanks

    Please don't hijack someone else's post...I will answer these questions here, but for future reference please start a new thread with questions beyond the scope of the original topic.

    Quote Originally Posted by javapenguin View Post
    Is it valid to have

    Scanner console = new Scanner(System.in);

    in the main method,
    Yes it is valid. Having it outside allows access to the class from other methods - like any other variable it has a scope.

    Quote Originally Posted by javapenguin View Post
    Also, why does main have to be like this:

    public static void main(String[] args)

    ?

    Why an array of Strings? Why not a Vector or an ArrayList of Strings?
    1) Vector or ArrayList requires an import, an array does not 2) these two objects didn't exist when java was being created and formed. 3) Maintains convention with other languages

    Quote Originally Posted by javapenguin View Post
    Also, what's to stop the programmer from creating two main methods?
    In the same class you will receive a compile time error. You can have multiple mains in different classes of the same project, just need to specify which one to use to the JVM.

  9. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (October 5th, 2010)

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help with homework program...thanks

    Quote Originally Posted by copeg View Post
    Please don't hijack someone else's post...I will answer these questions here, but for future reference please start a new thread with questions beyond the scope of the original topic.



    Yes it is valid. Having it outside allows access to the class from other methods - like any other variable it has a scope.



    1) Vector or ArrayList requires an import, an array does not 2) these two objects didn't exist when java was being created and formed. 3) Maintains convention with other languages


    In the same class you will receive a compile time error. You can have multiple mains in different classes of the same project, just need to specify which one to use to the JVM.
    Ah, I found a moderator. I was trying to find the FAQ that told me what the titles with each user are and how many posts you need to get them:

    How many do you need to get Java Kindergarten?

    How many do you need to get Junior Member?

    How many do you need to get Member?

    How many do you need to get Senior Member?

    How many do you need to get Elite Member?

    How many do you need to get Senile Half Wit?

    How many do you need to get Moderator?

    How many do you need to get Super Moderator?

    How many do you need to get Administrator?

    Are there any other levels I'm not aware of?

    Oh, and i think I answered the question of the creator of this thread anyway.

  11. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: need help with homework program...thanks

    Quote Originally Posted by javapenguin View Post
    Oh, and i think I answered the question of the creator of this thread anyway.
    It doesn't matter, you are still hijacking his post. The question in your last post should be made into another thread. Try posting it in The Cafe since it is not program related.

  12. The Following User Says Thank You to Brt93yoda For This Useful Post:

    javapenguin (October 5th, 2010)

Similar Threads

  1. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM
  2. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  3. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  4. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM