Jump to content

[JAVA] assignment help

Go to solution Solved by martward,
3 minutes ago, huracan said:

 

so i did manage to declare the string at the beginning but im getting a different kind of error. :(

 


Sandwich.java:39: error: variable sammichPrice might not have been initialized
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );

Heres how i declared it


String sammichType =new String() ;
String sammichSize = new String();

 

Yes so you have if ... else if statements, but no else. This means that in theory the values may not be initialized, for instance when the user enters "5" somewhere.

So either you should give the sammichType and sammischSize a default value (String sammichType =new String("Turkey Pesto"); ) or you replace the last else ifs for else's.

That way the variable is always initialized.

Alternatively you could make a while loop that loops until the user enters a value that actually represents something in the menu.

hi there when compiling im getting this error, please help me debug it

Sandwich.java:44: error: cannot find symbol
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );
	                                           ^
  symbol:   variable sammichSize
  location: class Sandwich
Sandwich.java:44: error: cannot find symbol
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );
	                                                         ^
  symbol:   variable sammichType
  location: class Sandwich
2 errors

HERES THE CODE:


import java.util.Scanner;
public class Sandwich{
   public static void main(String[] args){
	   int sizeInp;
	   int menuInp;
	   double sammichPrice;
	   Scanner sc = new Scanner(System.in);
	   
	   printMenu();
	   menuInp = sc.nextInt();
	   if (menuInp == 1){
			String sammichType = "Chicken Parmesan";
		}
		else if (menuInp == 2){
			String sammichType = "Turkey Pesto";
		}
		else if (menuInp ==3){
			String sammichType = "Breakfast Sandwich";

		}
		
	  printSizes();
	  sizeInp = sc.nextInt();
	  if (sizeInp == 1){
			String sammichSize = "half size";
			sammichPrice = 4.00;
		}
		else if (sizeInp == 2){
			String sammichSize = "Full size";
			sammichPrice = 6.50;
		}
		else if (sizeInp ==3){
			String sammichSize = "Overstuffed";
			sammichPrice = 8.00;
		}
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );
      //printMenu();
	  
      //printSizes();
      //printRegToppings();
      //printPremToppings();
      //printSauce();
      //printBread();

   } // end main
   
   
   
   
   public static void printMenu(){
      System.out.print("MENU: \n(Please select one of the following numerical options.)\n");
      System.out.print("1 Chicken Parmesan \n2 Turkey Pesto\n3 Breakfast Sandwich\n" +
         "4 Build your own\n");
   } // end printMenu

   public static void printSizes(){   
      System.out.print("\nSIZE:\n(Please select a size.)\n1 Half-size, $4.00\n2 Full-size, " +
         "$6.50\n3 Overstuffed, $8.00\n");
   } // end printSizes
   
   public static void printRegToppings(){
      System.out.print("\nREGULAR TOPPINGS:\nThe regular toppings are:\nTomato, Sprouts, " +
         "Mushroom, Cheddar, Red onion, Spinach, Jalapenos, Mozzarella\n\n" +
         "How many of these would you like?\n(Three are included and then " +
         "each additional regular topping costs $0.50.)\n");
   } // end printRegToppings
   
   public static void printPremToppings(){
      System.out.print("\nPREMIUM TOPPINGS:\nThe premium toppings are:\nChicken, Turkey, " +
         "Egg, Goat cheese, Grilled eggplant\n\n" +
         "How many of these would you like?\n(One is included and then " +
         " each additional premium topping costs $1.50.)\n");
   } // end printPremToppings
   
   public static void printSauce(){
      System.out.print("\nEXTRA SAUCE?:\nYour choice of sauce is included.\n" + 
         "1 Add extra sauce, $0.50 \n2 Regular amount of sauce\n");
   } // end printSauce
   
   public static void printBread(){
      System.out.print("\nARTISAN BREAD?:\nYour choice of white, whole wheat, or rye bread is included.\n" + 
         "1 Upgrade to artisan bread, $1.00 \n2 Regular bread\n");
   } // end printBread
   
} // end class

 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/
Share on other sites

Link to post
Share on other sites

11 minutes ago, robeng said:

You need to declare the variable sammichType and sammichSize at the start of the program. Right now the program creates a String sammichType and sammichSize, set's it to a value and then forgets it.

that makes sense, but where im confused is the declaring part, how can i declare a string variable at the start? do i just type "String sammichType;"  i think imma have to rework part of the code to use the print the result that way i want.

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979756
Share on other sites

Link to post
Share on other sites

4 minutes ago, huracan said:

that makes sense, but where im confused is the declaring part, how can i declare a string variable at the start? do i just type "String sammichType;"

You would do it just as anything else

String sammich = new String;

 

Or String sammichWhatever like you did with the price up top. Your objects haven't been instantiated yet, so they cannot be called in.

 

Or whatever you need.

 

sammichWhatever = "this string" fails because Java didn't know what to sammichWhatever is. It could be a Double, String, etc..., So it cannot sign anything to it or call it's reference because it didn't exist yet.

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979766
Share on other sites

Link to post
Share on other sites

5 minutes ago, huracan said:

that makes sense, but where im confused is the declaring part, how can i declare a string variable at the start? do i just type "String sammichType;"  i think imma have to rework part of the code to use the print the result that way i want.

Declare it like you do SammichPrice, I think that ought to do it (though with String offcourse).

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979768
Share on other sites

Link to post
Share on other sites

6 minutes ago, Ryujin2003 said:

You would do it just as anything else

String sammich = new String;

 

Or String sammichWhatever like you did with the price up top. Your objects haven't been instantiated yet, so they cannot be called in.

 

Or whatever you need.

 

sammichWhatever = "this string" fails because Java didn't know what to sammichWhatever is. It could be a Double, String, etc..., So it cannot sign anything to it or call it's reference because it didn't exist yet.

 

5 minutes ago, martward said:

Declare it like you do SammichPrice, I think that ought to do it (though with String offcourse).

so i did manage to declare the string at the beginning but im getting a different kind of error. :(

 

Sandwich.java:39: error: variable sammichPrice might not have been initialized
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );

Heres how i declared it

String sammichType =new String() ;
String sammichSize = new String();

 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979787
Share on other sites

Link to post
Share on other sites

3 minutes ago, huracan said:

 

so i did manage to declare the string at the beginning but im getting a different kind of error. :(

 


Sandwich.java:39: error: variable sammichPrice might not have been initialized
	  System.out.println("you have odered a" + sammichSize + sammichType + "sandwich. youre total is: " + sammichPrice );

Heres how i declared it


String sammichType =new String() ;
String sammichSize = new String();

 

Yes so you have if ... else if statements, but no else. This means that in theory the values may not be initialized, for instance when the user enters "5" somewhere.

So either you should give the sammichType and sammischSize a default value (String sammichType =new String("Turkey Pesto"); ) or you replace the last else ifs for else's.

That way the variable is always initialized.

Alternatively you could make a while loop that loops until the user enters a value that actually represents something in the menu.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979799
Share on other sites

Link to post
Share on other sites

6 minutes ago, martward said:

Yes so you have if ... else if statements, but no else. This means that in theory the values may not be initialized, for instance when the user enters "5" somewhere.

So either you should give the sammichType and sammischSize a default value (String sammichType =new String("Turkey Pesto"); ) or you replace the last else ifs for else's.

That way the variable is always initialized.

Alternatively you could make a while loop that loops until the user enters a value that actually represents something in the menu.

thank you for the help, it complied with out any errors and ran just the way i wanted it to. cheers to you man. also i shouldn't be using looping for this exercise as thats reserved for the next chapter...

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979816
Share on other sites

Link to post
Share on other sites

7 minutes ago, huracan said:

thank you for the help, it complied with out any errors and ran just the way i wanted it to. cheers to you man. also i shouldn't be using looping for this exercise as thats reserved for the next chapter...

Good to hear, good luck with the rest of the course :)

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10979835
Share on other sites

Link to post
Share on other sites

4 hours ago, martward said:

Good to hear, good luck with the rest of the course :)

hey im back with another question... so i have improved apon my code but im getting this error but i did initialize the variable :/

Sandwich1.java:101: error: variable sammichPrice might not have been initialized
		System.out.println("you have ordered a " + sammichSize + " " + sammichType + " sandwich. youre total is: " + "$" + sammichPrice );
import java.util.Scanner;
public class Sandwich1{
   public static void main(String[] args){
	  
	   int sizeInp;     //Declaration part
	   int menuInp;
	   double extraPrice;
	   double sammichPrice;
	   String sammichType =new String() ;
	   String sammichSize = new String();
	   Scanner sc = new Scanner(System.in);
	   
	   printMenu();
	   menuInp = sc.nextInt();
	
	if (menuInp == 4){	// ONLY CUSTOM SANDWICH PART 
	   
		
		printBread(); //BREAD PART
		int breadInp = sc.nextInt();
		if (breadInp ==1){
			extraPrice = 1;
		}
		else {
			extraPrice =0;
		}
			
		printSauce(); // SAUCE PART
		int sauceInp = sc.nextInt();
		if (sauceInp == 1){
			extraPrice = extraPrice + 0.5;
		}
		else{
			System.out.println("please only enter the given options");
		}
		printRegToppings();// TOPPING PART REGULAR
		double regToppingInp = sc.nextDouble();
		if (regToppingInp >3){
			regToppingInp = (regToppingInp - 3)*0.5 ;
			extraPrice = extraPrice + regToppingInp;
		}
		else if(regToppingInp <0){
			System.out.println("please enter only positive number of toppings");
		}	
		printPremToppings();//PREMIUM TOPPING PART
		double PremToppingInp = sc.nextDouble();
		if (PremToppingInp > 0){
			PremToppingInp = (PremToppingInp - 1) * 1.50;
			extraPrice = extraPrice + PremToppingInp;
			
		}
		else if(PremToppingInp < 0) {
			System.out.println("please enter only positive number of toppings");
		}
		
		printSizes();
		sizeInp = sc.nextInt(); //SIZE OF THE SANDWICH
		if (sizeInp == 1){
			sammichSize = "half-size";
			sammichPrice = 4.00;
		}
		else if (sizeInp == 2){
			sammichSize = "Full-size";
			sammichPrice = 6.50;
		}
		else {
			sammichSize = "Overstuffed";
			sammichPrice = 8.00;
		}
		System.out.println("you have ordered a " + sammichSize + " Custom sandwich. you're total is: " + "$" + (sammichPrice + extraPrice) );
		
				
	}		
	else if (menuInp <= 3){ // PRE-MADE SANDWICH PART
		 if (menuInp == 1){
			sammichType = "Chicken Parmesan";
		}
		else if (menuInp == 2){
			sammichType = "Turkey Pesto";
		}
		else if (menuInp ==3){
			sammichType = "Breakfast Sandwich";
		}
		
			
		printSizes();
		sizeInp = sc.nextInt();
		if (sizeInp == 1){
			sammichSize = "half-size";
			sammichPrice = 4.00;
		}
		else if (sizeInp == 2){
			sammichSize = "Full-size";
			sammichPrice = 6.50;
		}
		else if (sizeInp == 3) {
			sammichSize = "Overstuffed";
			sammichPrice = 8.00;
		}
		System.out.println("you have ordered a " + sammichSize + " " + sammichType + " sandwich. youre total is: " + "$" + sammichPrice );
		
        }
	else {
			System.out.println("please enter among only given options");
	}
   } 
   
   
   
   
   public static void printMenu(){
      System.out.print("MENU: \n(Please select one of the following numerical options.)\n");
      System.out.print("1 Chicken Parmesan \n2 Turkey Pesto\n3 Breakfast Sandwich\n" +
         "4 Build your own\n");
   } // end printMenu

   public static void printSizes(){   
      System.out.print("\nSIZE:\n(Please select a size.)\n1 Half-size, $4.00\n2 Full-size, " +
         "$6.50\n3 Overstuffed, $8.00\n");
   } // end printSizes
   
   public static void printRegToppings(){
      System.out.print("\nREGULAR TOPPINGS:\nThe regular toppings are:\nTomato, Sprouts, " +
         "Mushroom, Cheddar, Red onion, Spinach, Jalapenos, Mozzarella\n\n" +
         "How many of these would you like?\n(Three are included and then " +
         "each additional regular topping costs $0.50.)\n");
   } // end printRegToppings
   
   public static void printPremToppings(){
      System.out.print("\nPREMIUM TOPPINGS:\nThe premium toppings are:\nChicken, Turkey, " +
         "Egg, Goat cheese, Grilled eggplant\n\n" +
         "How many of these would you like?\n(One is included and then " +
         " each additional premium topping costs $1.50.)\n");
   } // end printPremToppings
   
   public static void printSauce(){
      System.out.print("\nEXTRA SAUCE?:\nYour choice of sauce is included.\n" + 
         "1 Add extra sauce, $0.50 \n2 Regular amount of sauce\n");
   } // end printSauce
   
   public static void printBread(){
      System.out.print("\nARTISAN BREAD?:\nYour choice of white, whole wheat, or rye bread is included.\n" + 
         "1 Upgrade to artisan bread, $1.00 \n2 Regular bread\n");
   } // end printBread
   
} // end class

 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10980407
Share on other sites

Link to post
Share on other sites

never mind i figured it out again its the same answer ... derp :/

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/890156-java-assignment-help/#findComment-10980411
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×