Results 1 to 5 of 5
Thread: Need help with loop:
- 03-07-2012, 01:50 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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 05:21 AM. Reason: code tagges added
- 03-07-2012, 02:59 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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.)
- 03-07-2012, 04:43 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
- 03-07-2012, 04:54 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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".
- 03-07-2012, 05:07 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 03:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 03:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 06:46 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 09:46 PM
Bookmarks