This site requires JavaScript, please enable it in your browser!
Greenfoot back
csr1010
csr1010 wrote ...

2014/11/18

All my variables and stuff are declared and everything seems to be fine, its just that eclipse doesn't think i have finished my loop, any help is appreciated.

csr1010 csr1010

2014/11/18

#
if(numSlices > 4)
			System.out.println("Too many slices");
		if(numSlices < 4 && choice.equals(P))
		{
			slicesP +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(C))
		{
			slicesC +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(E))
		{
			slicesE +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(Q))
		{
			Q = 1;
		}
		while(Q!=0);
Super_Hippo Super_Hippo

2014/11/18

#
Line 19 is a while loop in which you doesn't do anything (you only end it with the ; ), so if the loop is entered once, it will never break out of it.
csr1010 csr1010

2014/11/18

#
I'm sorry, I missed a part of the code, it is a "do while" loop, i forgot to show the other part.
	do{System.out.println("Choose type of pizza: ");
		choice = input.next();
		System.out.println("Enter number of slices: ");
		numSlices = input.nextInt();
			
		
			
		if(numSlices > 4)
			System.out.println("Too many slices");
		if(numSlices < 4 && choice.equals(P))
		{
			slicesP +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(C))
		{
			slicesC +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(E))
		{
			slicesE +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(Q))
		{
			Q = 1;
		}
		}
		while(Q!=0);
csr1010 csr1010

2014/11/18

#
Also line 26 on that last post isn't there anymore, don't get freaked out by the extra bracket
danpost danpost

2014/11/18

#
csr1010 wrote...
Also line 26 on that last post isn't there anymore, don't get freaked out by the extra bracket
It does not appear to be an extra one. I believe it is to match the one following 'do'.
csr1010 csr1010

2014/11/18

#
you are right. My apologies
csr1010 csr1010

2014/11/18

#
Here is the whole code, when i run this, it returns an error saying that my variables P, C, and E haven't been initialized.
import java.util.Scanner;
public class PizzaParty {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int numSlices = 0;
		String choice;
		int slicesP = 0;
		int slicesC = 0;
		int slicesE = 0;
		String P;
		String C;
		String E;
		int Q = 0;
		
		
		
		Scanner input = new Scanner(System.in);
		System.out.println("P - Pepperoni" );
		System.out.println("C - Cheese" );
		System.out.println("E - Everything" );
		System.out.println("Q - Quit" );
		
		
		do{System.out.println("Choose type of pizza: ");
		choice = input.next();
		System.out.println("Enter number of slices: ");
		numSlices = input.nextInt();
			
		
			
		if(numSlices > 4)
			System.out.println("Too many slices");
		if(numSlices < 4 && choice.equals(P))
		{
			slicesP +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(C))
		{
			slicesC +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(E))
		{
			slicesE +=numSlices;
		}
		else if(numSlices < 4 && choice.equals(Q))
		{
			Q = 1;
		}
		}
		while(Q!=0);
	}
}
		
jimboweb jimboweb

2014/11/18

#
You declared them but you never said what their values are. You created the String variables P, E and C, but you need to say something like P="pepperoni" or whatever it's supposed to be. Now that I look at it, it seems like you maybe want them to press the key "P" for pepperoni. But declaring the variable P doesn't mean the key "P". A variable's name could be anything, it doesn't have anything to do with what the variable actually represents.
danpost danpost

2014/11/18

#
@jimboweb, I think the intent was to use them (the P, C, E, and Q) as literal String values, but did not surround them in double-quotes. @csr1010, try replacing the do loop with this:
do
{
    System.out.println("Choose type of pizza: ");
    choice = (""+input.next()).toUpperCase();
    if (choice.length() != 1 || "PCEQ".indexOf(choice) < 0) continue;
    System.out.println("Enter number of slices: ");
    numSlices = input.nextInt();
    if (numSlices > 4) System.out.println("Too many slices");
    if (numSlices >= 4) continue;
    if ("P".equals(choice)) slicesP += numSlices;
    if ("C".equals(choice)) slicesC += numSlices;
    if ("E".equals(choice)) slicesE += numSlices;
    if ("Q".equals(choice)) break;
}
jimboweb jimboweb

2014/11/18

#
Thanks, danpost, that's what I was unsuccessfully trying to explain. There was no reason to make them variables at all.
danpost danpost

2014/11/18

#
jimboweb wrote...
Thanks, danpost, that's what I was unsuccessfully trying to explain. There was no reason to make them variables at all.
Yeah, I see where they were declared as String fields, but they were never set to anything. With my revised code, all the fields declared within lines 12 through 15 can be removed.
csr1010 csr1010

2014/11/19

#
@danpost @jimboweb I really appreciate your help guys.
You need to login to post a reply.