Results 1 to 17 of 17
  1. #1
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default hey im new to java, and i need some help with "while loops"

    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.


    Java Code:
    import javax.swing.JOptionPane;
    public class prog1
    {
    	public static void main(String Args[])
    	{
    		int ten;
    		ten=Integer.parseInt(JOptionPane.showInputDialog("Enter the number one"));
    		while (ten <= 10 )
    
    
    
    		{
    JOptionPane.showMessageDialog(null, + ten );
    		ten = ten + 1;
    
    
    }
    	}
    
    }
    here is the code, and thank you for your help
    Last edited by neilmx7; 12-07-2008 at 10:53 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    First show what you've tried and ask a more specific question, nobody here wants to deprive you of the pleasure of doing your own homework.

    This link can help you to get better help here:
    How To Ask Questions The Smart Way

    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

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

  4. #4
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java 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:

    outputString = outputString + your string here
    or
    outputString += your string here.

    You'll need to add a newline symbol to your string after the number is added, and this can be done with the "\n" string.

  8. #8
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    alright so i just ended up redoing the program all together, and heres what i got... it worked, thank you for all of your help, you are a lifesaver!

    Java Code:
    import javax.swing.JOptionPane;
    public class prog1
    {
    	public static void main(String Args[])
    	{
    
    int i = 1;
    String result = " ";
    
    
    while (i < 11)
    {
    
    result = result + i + "\n";
    i++;
    
    }
    JOptionPane.showMessageDialog(null,result);
          }
    }

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Congrats !!!

    A couple of comments:
    • 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:

    Java Code:
    }//end for
    or
    Java Code:
    }//end if
    or
    Java Code:
    }//end Sauropod class
    • FYI... the "while" loop in your program can be replaced by a "for" loop:

    Java 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    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!

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    A couple of comments:
    • Get in to the habit of identifying the closing brackets with comments:

    Java 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. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Interesting...

    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.
    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?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ah... now I understand

    ah... now I understand your comment about "shops" and "getting fired". Well, let's see...
    • 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    neilmx7 is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default 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?

    Java Code:
    import javax.swing.JOptionPane;
    public class prog1
    {
    	public static void main(String Args[])
    	{
    
    int i = 1;
    String result = " ";
    while (i < 11)
    {
    result = result + i + "\n";
    i++;
    }
    JOptionPane.showMessageDialog(null,result);
    
    int k = 1;
    String result2 = " ";
    
    while (k < 11)
    {
    	result2 = result2 + k + " ";
    	k++;
    }
    	JOptionPane.showMessageDialog(null,result2);
    
          int L = Integer.parseInt(JOptionPane.showInputDialog("Enter a number"));
          String result3 = " ";
    
          while (L < L++)
          {
    		  result3 = result3 + L + " ";
    			L++;
    		  
    	  }
    
    			JOptionPane.showMessageDialog(null,result3);
          }
    }

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default This is more complicated

    For starters...
    Java Code:
    while (L < L++)
    You can't compare L < L++, because L will always be smaller than L++
    Java Code:
    L++;
    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CJSLMAN View Post
    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
    Java Code:
    while (L < ++L)
    Again, sorry.

  17. #17
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  3. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  4. Using for loops to create a "bridge" made out of hyphens
    By carlodelmundo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 11:20 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •