Results 1 to 5 of 5
  1. #1
    dsp108 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Need help with loop:

    Java Code:
    import javax.swing.*;
    public class Spam
    {
    public static void main(String[] args)
       {
      String s;   
      s = JOptionPane.showInputDialog(" Please insert Spam");
        
      String j;
      j = JOptionPane.showInputDialog(" Please insert number of spam");
      
      int i = 1;
          do
          {
             System.out.println(s);
             i++;
          }
      [COLOR="#FF0000"][COLOR="#B22222"]while ((i = 10) + j);[/COLOR][/COLOR]
      JOptionPane.showMessageDialog(null, "you console was spammed " + s);
      
      
          System.exit (0);
       }
    
       }


    I am attempting to make it so it loops the input the user made at the number of times the user said. Sorry, im a beginner to java coding.
    Last edited by Eranga; 03-07-2012 at 04:21 AM. Reason: code tagges added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with loop:

    The thing in while part has to be a condition like "i is less than j". I'm not sure what condition you have there.

    (In general, if the compiler grumbles and you can't understand the message, post the message and say what line it refers to.)

  3. #3
    dsp108 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help with loop:

    Quote Originally Posted by pbrockway2 View Post
    The thing in while part has to be a condition like "i is less than j". I'm not sure what condition you have there.

    (In general, if the compiler grumbles and you can't understand the message, post the message and say what line it refers to.)
    yes, the error is
    File: C:\Users\admin\Desktop\mods\Spam.java [line: 18]
    Error: Type mismatch: cannot convert from java.lang.String to boolean

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with loop:

    Yes - it's a rather cryptic message. [i]j[ij] is a String, and i=10 is an int. So the compiler will interpret (i=10)+j as a string. (Something like "10666" if the number of spam was entered as the string "666"). Now a string is not a boolean (the compiler's name for a condition) so you get the message.

    Don't try and understand all that. Although the bit about the response to the "please insert a number" prompt being an String was interesting. Instead focus on replacing the condition you had with something like the condition "i is less than j".

  5. #5
    dsp108 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need help with loop:

    I can get it with out the error, but I cannot make it so that I have the input from the user say the number for the amount of loops it makes. Does anyone know how to do that?

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Posting Permissions

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