Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-15-2007, 09:55 AM   PM User | #1
stu_villanti
New to the CF scene

 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
stu_villanti is an unknown quantity at this point
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
stu_villanti is offline   Reply With Quote
Old 06-15-2007, 03:20 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 06-15-2007, 07:20 PM   PM User | #3
stu_villanti
New to the CF scene

 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
stu_villanti is an unknown quantity at this point
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");
}
}
}
stu_villanti is offline   Reply With Quote
Old 06-15-2007, 10:09 PM   PM User | #4
javabits
New Coder

 
Join Date: May 2007
Location: SF, CA
Posts: 55
Thanks: 0
Thanked 4 Times in 4 Posts
javabits is on a distinguished road
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");
        }
    }
}
javabits is offline   Reply With Quote
Old 06-16-2007, 05:22 AM   PM User | #5
stu_villanti
New to the CF scene

 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
stu_villanti is an unknown quantity at this point
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
stu_villanti is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:37 AM.


Advertisement
Log in to turn off these ads.