hey everybody, this is my first post, and i am having a pretty tough time with a while loop. I am currently taking a course in java, and i cant figure out how to do this assignment, if somebody could help me, i would really appreciate it.

the assignment is

Design a program that prints all values from 1 to 10. the program should show the result in an output dialog box with each number appearing on a separate line.

When the user clicks the OK button of the dialog box, the program shows in a dialog box all values from 1 to 10 on the same line separated by one space.

when the user click the OK button of the previous step, the user will be asked for a number in an input dialog box. The program proceeds to print all values from 1 up to the number that the user entered, all on separate lines

when the user clicks the OK button of the dialog box, the user will once again be asked for a vlaue. The program shows all values from one up to the number entered in reverse order all on one line.

now when i tried it, i would get it to loop, but the numbers would print in seperate dialog boxes. but i need it to print in just one box on seperate lines.

I have to agree with Darryl and that we have no way of knowing what your misconceptions are without first seeing your code attempt.

Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

Code:

&#91;code]
// your code block goes here.
// note the differences between the tag at the top vs the bottom.
&#91;/code]

12-07-2008, 11:51 PM

neilmx7

i have added the code, and thank you for showing me how to put in the code!

12-08-2008, 12:01 AM

Fubarable

ah, your while loop is fine. Your problem is with the output. Instead of displaying the output each time the loop goes around, that is displaying it from within the loop, why not create a String before the loop, add to the String from within the loop, and then display the string after the loop? Do you know why it is important that the String be created before the loop and displayed afterward? Good luck and HTH.

Edit: note that purists may tell you to use a StringBuffer here instead of a String, but with a small simple loop like this, there is no harm in using a String, and I think it will be simpler for you to do so.

12-08-2008, 12:21 AM

neilmx7

i understand what you are saying, but i am unsure of how to code that, are you saying that before the loop, add the "JOptionPane.showMessageDialog(null, + ten );", then add it again within the loop?

12-08-2008, 12:29 AM

Fubarable

Right now, your JOptionPane will show 10 times, each time showing a number from 1 to 10. That makes sense since the JOptionPane is being called from within the loop, and that loop will run 10 times, right? What you want to do is to create a String, say call it

Code:

outputString = ""; //make it empty to start with

and then inside your loop add a String that has your number to this output String. This can be done one of two ways, either:

Your program is a short one (easy to see the opening/closing brackets), but as they grow it becomes dificult to know which bracket is the closing bracket. Get in to the habit of identifying the closing brackets with comments:

Code:

}//end for

or

Code:

}//end if

or

Code:

}//end Sauropod class

FYI... the "while" loop in your program can be replaced by a "for" loop:

Code:

for (int i=0;i < 11;i++)
{
result = result + i + "\n";
}

Notice that it has the initializer (i=0) and the incrementor (i++) all in the same statement.

Luck,
CJSL

12-08-2008, 05:30 AM

neilmx7

thanks man, but its funny that you say that about the for loops, because thats actually the next assignment, i have to re write the program using a for loop also... thanks to all for your help, you guys rock!

12-08-2008, 07:00 AM

DarrylBurke

Quote:

Originally Posted by CJSLMAN

A couple of comments:

Get in to the habit of identifying the closing brackets with comments:

Code:

}//end for

<snip/>
CJSL

CJSL, you're a student, aren't you? or maybe a teacher? Insisting on writing meaningless comments like that could cost you your job in most shops.

The correct way to match up braces is with appropriate indentation. And most IDEs will highlight the matching brace/parenthesis/bracket/angle bracket anyways.

db

12-08-2008, 03:47 PM

CJSLMAN

Interesting...

Quote:

CJSL, you're a student, aren't you? or maybe a teacher? Insisting on writing meaningless comments like that could cost you your job in most shops.

I'm neither. Meaniningless? Not sure I undertand what you mean. I consire it a good practice and costs no effort.

Quote:

The correct way to match up braces is with appropriate indentation. And most IDEs will highlight the matching brace/parenthesis/bracket/angle bracket anyways.

Yes I totally agree that indentation is important and the IDEs that I've seen do match the brackets. I also find this technique useful.
Now that I've answered your comments, I have a question:

How in the world did I get on your wrong side with a comment that was intented to help ? It would appear that this pent up anger has been festering for some time.... any reason why?

The code that is being presented doesn't look too "shop" oriented to me. More like, "classroom" oriented.

Don't know if the OP is using an IDE.

I'll tell you what.... let's go 50/50 on this one:

I'll tack on the word "suggestion" from here on because "get in the habit" does sound rather imperative (I'll remember this particular case)

You can say something in the future like "those tags are OK for class assignments, but in the professional world it's not necessary (would look bad) because IDEs do it for you"

CJSL

12-08-2008, 11:30 PM

neilmx7

alright guys, sorry if im bothering you again, i need help with one more thing

This is the code i have so far for the whole assignment. i am stuck at part 3. at this point, the program asks the user for a value, and then print all numbers leading up to that number on seperate lines. when i tried it, i would enter a number in the dialog box, and then the output would just be a blank dialog box. what am i doing wrong?

You're not supposed to increment L, that's your target (if it increments, you're never going to catch up with it).

You have to compare L against result3, but .... you can't because L is an int and result3 is a string (int != string). That's why it gets a little more complicated.

.. and before this gets too messy, please name your variables so they mean something. A capital L does not indicate anything... is it an int, a string, a bird, etc.? Suggestion... call it intAnswer3 or iTarget3 (or whatever... so long it means something).

So the challenge is going to be: how compare those two variables?

Homework: find out how to convert an int to a string (Hint: look for a method in the Integer class).

Luck,
CJSL

12-09-2008, 02:32 AM

Fubarable

Quote:

Originally Posted by CJSLMAN

For starters...
You can't compare L < L++, because L will always be smaller than L++

I hate to nitpick, but L will always == L++. Maybe you're thinking of

Code:

while (L < ++L)

Again, sorry.

12-09-2008, 03:05 AM

CJSLMAN

hey no problem...

hey no problem ... no need to be sorry... thanks for the observation... and yes, you are right. At the moment of the comparition, L++ it still has it's original L value. It doesn't increment until after the comparation operation is done.

@ Neil: you still can't compare L with itself (with or without the increment).