Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: while loop

  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    while loop

    hi, Ok i am new to java programming. I am currently trying to write a new program which enters awhile loop waiting for s,g,l to be entered to e to exit the loop.

    i have been trying to with no luck.

    any help of where to start what to declare, programming need.

    using methods if that help

    thanks

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Well, what have you written so far? Do you need a full on tutorial or do you just need help in the while loop area?

    Sun provides a great tutorial on java here: ;url="http://java.sun.com/docs/books/tutorial/"]Sun Tutorial[/url]

    To which they talk about while loops here:
    While Loop

    And talk about String Equals here:
    String Comparison

    We aren't going to do your assignment for you (as per the rules), but if you write something we can help you along the way.

    And if you're not beginning just look a tthe while loop and String Equals part. That should help the most.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    so far

    import javax.swing.*;
    public class Garden
    {
    static String product;
    int a,b,c,d;

    public static void main (String [] args )
    {
    enterProduct();
    System.exit(0);
    }

    public static void enterProduct()
    {
    product=JOptionPane.showInputDialog(null,"Enter number e to exit");

    while (product !="e")
    {
    product=JOptionPane.showInputDialog(null,"Enter number 3 to exit");
    }
    }
    }

  • #4
    New Coder
    Join Date
    May 2007
    Location
    SF, CA
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    When you are comparing strings in java you need to use the comparison method equals. If you are comparing primitive data types then you would use !=, or ==.

    So the program below is syntactically correct and will exit if you enter e. Not quite sure if this is logically correct from your description of what the program needs to do though.

    semper fi...

    Code:
    import javax.swing.*;
    public class Garden
    {
        static String product;
        int a,b,c,d;
    
        public static void main (String [] args )
        {
            enterProduct();
            System.exit(0);
        }
    
        public static void enterProduct()
        {
            product=JOptionPane.showInputDialog(null,"Enter e to exit");
    
            while (!product.equals( "e" ))
            {
                product=JOptionPane.showInputDialog(null,"Enter e to exit");
            }
        }
    }

  • #5
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Not sure is i was to clear with what is sed.

    here is my is code so for

    import javax.swing.*;
    public class Garden
    {
    static String product, item, ordered,s, l, g, w, e;
    static double SAND=14.45, GRAVEL=16.75, LOAM=17.20;
    int s, l, g, w, e;

    public static void main ( String [] args )
    {
    enterProduct();
    askAmount();
    System.exit( 0 );
    }

    public static void enterProduct()
    {
    product = JOptionPane.showInputDialog( "BEDROCK LANDSCAPE SUPPLIERS \n\n S:Sand \n L: Loam \n G: Gravel \n W: Woodchips \n\n\n E: End \n Enter Supply Code" );
    item= product + item;

    while ( !product.equals( "e" ) )
    {
    product = JOptionPane.showInputDialog( "BEDROCK LANDSCAPE SUPPLIERS \n\n S:Sand \n L: Loam \n G: Gravel \n W: Woodchips \n\n\n E: End \n Enter Supply Code" );
    item = product + item;
    }
    }

    public static void askAmount()
    {
    if(product ="s")
    temp=JOptionPane.showInputMessage(null,"How Much Sand do you want?");
    ordered=Double.parseDouble(temp);
    ordered= SAND * ordered;
    }
    else
    {
    if(product = "g")
    ordered=Double.parseDouble(temp);
    ordered= GRAVEL * ordered;
    }
    }


    }

    trying to use what was entered in the first string to ask for amount ordered the work out total.

    thanks


  •  

    Posting Permissions

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