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

Thread: Unknown problem with output

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Unknown problem with output

    Hey guys,

    I currently have the following code(look below). When program is ran no errors occur, however the output isn't correct, and i've spent the past few hours trying to figure out but can't. So i've given up trying now aha, if anyone has any idea that'd be great.

    Here is the output:
    VVCap Image
    As you see the toppings are completely messed up, no idea why. Any guidance appreciated.
     
    	/*
    	*/
    	import java.util.Scanner;
    import java.text.DecimalFormat;
     
    	public class Pizza
    	{
     
     
     
     
    		public Pizza(String string, int pepCost) {
    			// TODO Auto-generated constructor stub
    		}
     
     
    			public static void main(String [ ] args)
    		{
     
    			//Create a Scanner object to read input
    			Scanner keyboard = new Scanner (System.in);
     
    			String Name;					//user's first name
    			int inches;							//size of the pizza
    			String crustType;					//code for type of crust
    			String crust = "Hand-tossed"; //name of crust
    			double cost = 12.99;				//cost of the pizza
    			final double TAX_RATE = .08;	//sales tax rate
    			double tax;							//amount of tax
    			char choice;						//user's choice
    			String input;						//user input
    			String toppings = "Cheese ";	//list of toppings	
    			int numberOfToppings = 0;		//number of toppings
    			String Address; 
     
    			//prompt user and get first name
    			System.out.println("Dominos Pizza ");
    			System.out.print("Enter your first name:  ");
    			Name = keyboard.nextLine();
    			System.out.print("Please, enter your delivery address:  ");
    			Address = keyboard.nextLine();
     
     
    			//determine if user is eligible for discount by
    			//having the same first name as one of the owners
    			//ADD LINES HERE FOR TASK #1
     
     
     
    		    //Pizza Costs
    			int pepCost = 10;
    			int ctCost = 8;
    			int sfCost = 12;
    			//prompt user and get pizza size choice
     
    			Pizza Pep = new Pizza("Pepperoni Pizza", pepCost);
    			Pizza ct = new Pizza("Pepperoni Pizza", ctCost);
    			Pizza seafood = new Pizza("Pepperoni Pizza", sfCost);
     
     
    			//Topping Costs
     
    			double bCost = 1.75;
    			double cCost = 1.75;
    			double eCost = 1.75;
    			double pCost = 1.75; 
    			//*****************Topping objects***************************** 
     
    			Toppings Bacon = new Toppings("Bacon",bCost);	
    			Toppings Cheese = new Toppings("Cheese",cCost);
    			Toppings Egg = new Toppings("Egg",eCost);
    			Toppings prawn = new Toppings("Prawn",pCost);
     
    		//*****CRUST COSTS*************************
     
    			int thinCost = 1; 
    			double deepCost = 1.50;
    		//*****CRUST TYPES*************************
     
    		CrustType thin = new CrustType("thin", thinCost);
    		CrustType deepDish = new CrustType("DeepDish", deepCost);
     
    		//***********USER INPUT**************	
    		System.out.println("What is your preference of crust?");
    		System.out.println(thin);
    		System.out.println(deepDish);
    		System.out.println("Choose your desired crust type");
    		crustType = keyboard.nextLine();
     
     
     
     
     
    			System.out.print("Do you want"+
    			Bacon +
    			"?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'Y' || choice == 'y')
    			{
    				numberOfToppings += 1;
    				toppings = toppings + "Bacon ";
    			}
    			System.out.print("Do you want"+
    					Cheese +
    					"?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'Y' || choice == 'y')
    			{
    				numberOfToppings += 1;
    				toppings = toppings + "Cheese ";
    			}
    			System.out.print("Do you want"+
    					Egg +
    					"?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'Y' || choice == 'y')
    			{
    				numberOfToppings += 1;
    				toppings = toppings + "Egg";
    			}
    			System.out.print("Do you want"+
    					prawn +
    					"?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'Y' || choice == 'y')
    			{
    				numberOfToppings += 1;
    				toppings = toppings + "Prawn ";
    			}
     
     
     
    			//display order confirmation
    			System.out.println();
    			System.out.println("Confirmation order: ");
    			//System.out.println(inches + " inch pizza");
    			System.out.println(crust + " crust");
    			System.out.println(toppings);		
     
    		//*******************WORKING OUT COST**********************************************************
    			System.out.println("Total cost: £" + cost);
    			cost = cost + (1.25*numberOfToppings);
    			//calculate and display tax and total cost
     
     
    			System.out.println("Toal cost:" + (cost+1.75*numberOfToppings));
     
    			System.out.println("Thank you for shopping, with Dominos.");
    		}	
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unknown problem with output

    the output isn't correct
    Can you post the output and add some comments saying what is wrong and show what the output should be?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    maths94 (November 28th, 2013)

  4. #3
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Unknown problem with output

    I had attached a weblink that shows the output.

    Dominos Pizza
    Enter your first name: js
    Please, enter your delivery address: s
    What is your preference of crust?
    CrustType@41171d93
    CrustType@5496bf47
    Choose your desired crust type
    s
    Do you wantToppings@74e7f9b3?y
    Do you wantToppings@17f74864?y
    Do you wantToppings@6bb1a986?Y
    Do you wantToppings@5cf0d3a6?y

    Confirmation order:
    Hand-tossed crust
    Cheese Bacon Cheese EggPrawn
    Total cost: £12.99
    Toal cost:24.990000000000002
    Thank you for shopping, with Dominos.

    I understand that the crust bit won't work because i hadn't really done that properly. However, the toppings shouldn't be displaying @ then a load of random numbers. I've no idea why..

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unknown problem with output

    Toppings@74e7f9b3
    That is the String returned by the default toString() method for the Object class. If you want to see something different for the Toppings class, override its toString() method and have it return the String you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    maths94 (November 28th, 2013)

  7. #5
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Unknown problem with output

    Quote Originally Posted by Norm View Post
    That is the String returned by the default toString() method for the Object class. If you want to see something different for the Toppings class, override its toString() method and have it return the String you want to see.
    Don't really understand what you mean. However, i've putten

    CrustType thin = new CrustType("thin", thinCost);
    CrustType deepDish = new CrustType("DeepDish", deepCost);

    //***********USER INPUT**************
    / System.out.println("What is your preference of crust?");
    System.out.println(thin);
    System.out.println(deepDish);

    System.out.println("Choose your desired crust type");
    crustType = keyboard.nextLine();

    The lines i've underlined are referring to the two otherlines above the block of code.
    CrustType thin = new CrustType("thin", thinCost);
    CrustType deepDish = new CrustType("DeepDish", deepCost);

    So it should display "thin" and the cost of thin and "DeepDish" and cost of DeepDish =/

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unknown problem with output

    Add a method named: toString() that returns the String you want to see to those classes which you are using in the println() methods.
    If you don't understand my answer, don't ignore it, ask a question.

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

    maths94 (November 28th, 2013)

  10. #7
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Unknown problem with output

    Quote Originally Posted by Norm View Post
    Add a method named: toString() that returns the String you want to see to those classes which you are using in the println() methods.

    public void toString (Bacon, Cheese, Egg, Prawn, bCost, eCost,pCost){
    }

    When i do this i get void is an invalid type for the variable toString. But, when i try without i still receive errors. Also, all the parameters receiving errors. All toppings are required to be objects btw

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Unknown problem with output

    Look at the Object class for how to define the toString() method.
    It returns a String.
    It does NOT take any args.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. unknown error
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 29th, 2013, 07:51 AM
  2. unknown problems
    By kindk12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 5th, 2012, 01:50 PM
  3. problem with output
    By Timur in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 11th, 2011, 09:56 AM
  4. [SOLVED] Unknown Character
    By aussiemcgr in forum Java Theory & Questions
    Replies: 19
    Last Post: September 1st, 2010, 05:22 PM
  5. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM