Results 1 to 11 of 11
  1. #1
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default New w/ working with my own classes and main site, with demo - while loop not working

    I am fairly new with Java. I am in my first programming class. We are working on our own classes and the below is the main program from a set of two (one poles from the other) and for some reason, when I volunteered today to present my homework, my while statement did not work. (It worked earlier the night before) I was not using my own computer when presenting. The "while" statement should send for validation if someone enters a number outside of 1,2,3 or 4. NOW it won't even let me past this question and is not recognizing a correct answer for input. Can someone test it and see if it works? I have copied and pasted BELOW this main program, the one that works with it. I thought maybe it was my Eclipse program, but it doesn't seem to work even when I RECODE it all from scratch in another program... any ideas?

    [code = java]

    import javax.swing.JOptionPane;


    public class OtherShoes1 {
    public static void main(String[] args) {

    String name, brand, color, material, width, strSize, strPrice, style;
    double size, price;

    //specifying what class object is created from // USE FOR NON ARG CONSTRUCTOR
    Shoes1 shoeChoice1 = new Shoes1();

    name = JOptionPane.showInputDialog("Please enter your name: ");
    style = JOptionPane.showInputDialog("Please enter a style number:"
    + " \n1 - Tennis Shoes \n2 - Pumps \n3 - Flats \n4 - Boots");
    int iStyle = Integer.parseInt(style);

    while (iStyle != 1 || iStyle != 2 || iStyle != 3 || iStyle != 4)
    {
    JOptionPane.showMessageDialog(null, "The style number must be a 1, 2, 3 or 4."
    + "\nPlease re-enter an appropriate style number.");

    style = JOptionPane.showInputDialog("Please enter a style number:"
    + " \n1 - Tennis Shoes \n2 - Pumps \n3 - Flats \n4 - Boots");
    iStyle = Integer.parseInt(style);
    }

    brand = JOptionPane.showInputDialog("If you have a brand preference, "
    + "please enter it here: ");
    material = JOptionPane.showInputDialog("Please enter the material of "
    + "your choice: ");
    color = JOptionPane.showInputDialog("Please enter the color of "
    + "your choice: ");
    width = JOptionPane.showInputDialog("Please enter a width: ");
    strSize = JOptionPane.showInputDialog("Please enter your size: ");
    strPrice = JOptionPane.showInputDialog("Please enter the price of "
    + "your shoe selection: ");

    size = Double.parseDouble(strSize);
    price = Double.parseDouble(strPrice);

    shoeChoice1.setName(name); // instance methods
    shoeChoice1.setBrand(brand);
    shoeChoice1.setMaterial(material);
    shoeChoice1.setColor(color);
    shoeChoice1.setWidth(width);
    shoeChoice1.setSize(size);
    shoeChoice1.setPrice(price);
    shoeChoice1.setStyle(style);

    // USE FOR ARG - REGULAR CONSTRUCTOR

    name = JOptionPane.showInputDialog("Please enter your name: ");
    style = JOptionPane.showInputDialog("Please enter a style number:"
    + " \n1 - Tennis Shoes \n2 - Pumps \n3 - Flats \n4 - Boots");
    iStyle = Integer.parseInt(style);
    while (iStyle != 1 || iStyle != 2 || iStyle != 3 || iStyle != 4)
    {
    JOptionPane.showMessageDialog(null, "The style number must be a 1, 2, 3 or 4."
    + "\nPlease re-enter an appropriate style number.");

    style = JOptionPane.showInputDialog("Please enter a style number:"
    + " \n1 - Tennis Shoes \n2 - Pumps \n3 - Flats \n4 - Boots");
    iStyle = Integer.parseInt(style);

    }

    brand = JOptionPane.showInputDialog("If you have a brand preference, "
    + "please enter it here: ");
    material = JOptionPane.showInputDialog("Please enter the material of "
    + "your choice: ");
    color = JOptionPane.showInputDialog("Please enter the color of "
    + "your choice: ");
    width = JOptionPane.showInputDialog("Please enter a width: ");
    strSize = JOptionPane.showInputDialog("Please enter your size: ");
    strPrice = JOptionPane.showInputDialog("Please enter the price of "
    + "your shoe selection: ");

    size = Double.parseDouble(strSize);
    price = Double.parseDouble(strPrice);

    // creating object shoeChoice2 and setting all values at once
    Shoes1 shoeChoice2 = new Shoes1(name, brand, color, material, width, style, size, price);

    DecimalFormat formatter = new DecimalFormat("$#.00");


    JOptionPane.showMessageDialog(null, "ORDER SUMMARY: \n\nNAME: "
    + shoeChoice1.getName()
    + "\nBRAND #1: " + shoeChoice1.getBrand()
    +"\nMATERIAL #1: " + shoeChoice1.getMaterial()
    + "\nCOLOR #1: " + shoeChoice1.getColor()
    + "\nWIDTH #1: " + shoeChoice1.getWidth()
    + "\nSIZE #1: " + shoeChoice1.getSize()
    + "\nSTYLE #1: " + shoeChoice1.getStyle()
    + "\nPRICE #1: " + formatter.format(shoeChoice1.getPrice())
    + "\nTAX COST: " + formatter.format(shoeChoice1.calcTaxCost()) // feeding return value from taxcost//
    //into totalcost method//
    + "\nTOTAL COST: " + formatter.format(shoeChoice1.calcTotalCost(shoeCho ice1.calcTaxCost())));



    JOptionPane.showMessageDialog(null, "ORDER SUMMARY: \n\nNAME: "
    + shoeChoice2.getName()
    + "\nBRAND #2: " + shoeChoice2.getBrand()
    +"\nMATERIAL #2: " + shoeChoice2.getMaterial()
    + "\nCOLOR #2: " + shoeChoice2.getColor()
    + "\nWIDTH #2: " + shoeChoice2.getWidth()
    + "\nSIZE #2: " + shoeChoice2.getSize()
    + "\nSTYLE #2: " + shoeChoice2.getStyle()
    + "\nPRICE #2: " + formatter.format(shoeChoice2.getPrice())
    + "\nTAX COST: " + formatter.format(shoeChoice2.calcTaxCost())
    + "\nTOTAL COST: " + formatter.format(shoeChoice2.calcTotalCost(shoeCho ice2.calcTaxCost())));

    }
    }
    [/code]
    Java Code:
    public class Shoes1 {
    		private double size, price;
    		private String name, brand, color, material, width, style;
    		
    		// no arg constructor
    		
    		public Shoes1()// initializes object
    		{
    			/*name = "";
    			brand = "";
    			color = "";
    			material = "";
    			width = "";
    			style = "";
    			size = 0;
    			price = 0;	*/
    		}
    		//  regular constructor
    		public Shoes1(String theName, String theBrand, String theColor, String theMaterial, String theWidth, String theStyle, double theSize, double thePrice)
    		{
    			name = theName;
    			brand = theBrand;
    			color = theColor;
    			material = theMaterial;
    			width = theWidth;
    			style = theStyle;
    			size = theSize;
    			price = thePrice;
    		}
    		public void setName(String theName)
    		{
    			name = theName;
    		}
    		public void setBrand(String theBrand)
    		{
    			brand = theBrand;
    		}
    		public void setColor(String theColor)
    		{
    			color = theColor;
    		}
    		public void setMaterial(String theMaterial)
    		{
    			material = theMaterial;
    		}
    		public void setWidth(String theWidth)
    		{
    			width = theWidth;
    		}
    		public void setStyle(String theStyle)
    		{
    			switch(theStyle)
    			{
    			case "1":	style = "Tennis Shoes"; break;
    			case "2":	style = "Pumps"; break;
    			case "3":	style = "Flats"; break;
    			case "4":	style = "Boots"; break;
    			default:	break;
    			}
    		}
    		public void setSize(double theSize)
    		{
    			size = theSize;
    		}
    		public void setPrice(double thePrice)
    		{
    			price = thePrice;
    		}
    		public String getName()
    		{
    			return name;
    		}
    		public String getBrand()
    		{
    			return brand;
    		}
    		public String getColor()
    		{
    			return color;
    		}
    		public String getMaterial()
    		{
    			return material;
    		}
    		public String getWidth()
    		{
    			return width;
    		}
    		public String getStyle()
    		{
    			return style;
    		}
    		public double getSize()
    		{
    			return size;
    		}
    		public double getPrice()
    		{
    			return price;
    		}								
    		public double calcTaxCost()
    		{
    			double taxPercent = .08;
    			double taxCost;
    			taxCost = price * taxPercent;
    			return taxCost;
    		}								
    		public double calcTotalCost(double taxCost)
    		{
    			double totalCost; 
    			totalCost = price + taxCost;
    			return totalCost;	
    		}
    		
    
    	}
    Last edited by Lorelei7; 11-22-2013 at 02:25 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Which while statement is the problem? I see more than one.

    Java Code:
    while (iStyle != 1 || iStyle != 2 || iStyle != 3 || iStyle != 4)
    When using OR operators in conditions, the full condition will be true if any ONE of the sub conditions is true.
    If an AND operator is used, then ALL sub conditions must be true for the full expression to be true.
    Last edited by Norm; 11-22-2013 at 01:33 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    Norm, thanks for a quick reply. I have to figure out how to tag it and then I will - Both while statements should be the same, so it doesn't matter - the first is the one that wasn't working to begin with.. If I enter "1", it automatically goes to the validation instead of going on to ask the other queries.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    You need to change the condition in the while statement so it is only true when the data is not what you want.

    If I enter "1",
    Then this part of the condition is true: iStyle != 2
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    Well - it says that while entry is not equal to 1 or etc... I want it to work when something outside of the 1-4 is used. But it starts re-asking the "validation" part even when a "1" (which is a correct response) is entered.

  6. #6
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    in an OR test .....if any return FALSE, it should kill the loop, right?
    Last edited by Lorelei7; 11-22-2013 at 02:20 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    The other way. With OR, if ANY ONE sub condition in the full condition is true, the full condition is true.
    There could be 9 false and one true and the full condition would return true.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    Norm, for some reason, my husband (who does programming - but not Java) and I believe it WAS working last night as while (iStyle != 1 || iStyle != 2 || iStyle != 3 || iStyle != 4).
    We revised it to: while (iStyle < 1 && iStyle > 4) -- and it is working. However, he is very concerned that if this above worked last night and does not now, there is a problem.
    Last edited by Lorelei7; 11-22-2013 at 02:53 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    (iStyle < 1 && iStyle > 4)
    When is a number BOTH < 1 AND > 4?

    To get a feel for what the value of the condition will be, write a two line program:
    Java Code:
    int iStyle = x;
    System.out.println(iStyle < 1 && iStyle > 4) ;
    replace x with an int value, compile and execute the code and see what the results are. Change x and do it again.
    Last edited by Norm; 11-22-2013 at 02:51 AM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    Okay, we changed it to OR... the program is functioning fine now. Thanks so much for your help. For some crazy reason only the second program is formatting right though I DID tag them both I thought. Anyway, I found my go to place for help. I appreciate it so much! God bless you!

    Laura

  11. #11
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: New w/ working with my own classes and main site, with demo - while loop not work

    perhaps istyle never equals 1 - 2 etc.

Similar Threads

  1. while loop not working
    By Christa912 in forum New To Java
    Replies: 6
    Last Post: 09-10-2013, 08:17 PM
  2. My while loop is not working, why??
    By esined93 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 01:05 AM
  3. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 10:36 PM
  4. Replies: 59
    Last Post: 05-22-2011, 08:37 AM
  5. HELP working with 2 classes!!! URGENT!
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •