Page 1 of 2 12 LastLast
Results 1 to 20 of 36
Like Tree1Likes

Thread: How to loop this????

  1. #1
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default How to loop this????

    Java Code:
    import javax.swing.*;
     
    public class Loop {
    	public static void main(String[] args){
     
    		int n=0;
    		String word="";
     
    		do{
    			word = JOptionPane.showInputDialog("Enter a Number: ");
    			n = Integer.parseInt(word);
     
    			if( n == 21 ){
    				JOptionPane.showMessageDialog(null, "TV5");
    			}
    			else if( n == 12 ){
    				JOptionPane.showMessageDialog(null, "GMA");
    			}
    			else if( n == 2 ){
    				JOptionPane.showMessageDialog(null, "STUDIO 23");
    			}
    			else if( n == 23 ){
    				JOptionPane.showMessageDialog(null, "ABC-CBN");
    			}
    			else if( n == 5 ){
    				JOptionPane.showMessageDialog(null, "IBC");
    			}
    			else{
    				JOptionPane.showMessageDialog(null, "Invalid Number!");
    				word = JOptionPane.showInputDialog(null,"Utro Mali imong ");
    			}
    		}while((n<=5) && (n>=0));
    	}
    }
    Last edited by sunde887; 08-26-2011 at 03:50 PM. Reason: Added code tags, [code]...[/code]
    lolomosaw likes this.

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

    Default

    You've already abandoned the two threads you started earlier. Why take the trouble to ask questions in a forum when you can't be bothered to follow up?

    db

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Your code has a loop, so I don't understand your question.

  4. #4
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
     
    public class Loop {
    	public static void main(String[] args){
     
    		int n;
    		String word="";
     
    		do{
    			word = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE);
    			n = Integer.parseInt(word);
     
    			if( n == 21 ){
    				JOptionPane.showMessageDialog(null, "TV5","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 12 ){
    				JOptionPane.showMessageDialog(null, "GMA","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 2 ){
    				JOptionPane.showMessageDialog(null, "STUDIO 23","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 23 ){
    				JOptionPane.showMessageDialog(null, "ABC-CBN","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 10 ){
    				JOptionPane.showMessageDialog(null, "IBC","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 5 ){
    				JOptionPane.showMessageDialog(null, "RPN","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else if( n == 35 ){
    				JOptionPane.showMessageDialog(null, "QTV","YOU'RE ON THE CHANNEL:",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else{
    				JOptionPane.showMessageDialog(null, "KARAT ANG CHANNEL","ANG CHANNEL KAY",JOptionPane.ERROR_MESSAGE);
    			n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    			if(n == JOptionPane.NO_OPTION) 
    			System.exit(0);}
    		}while(n==JOptionPane.YES_OPTION);
    	}
    }
    HAVE YOU SEE THE WHILE... what code will be back to word = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE); and continue to LOOP if i choose YES... i already check that if no it will be exit my problem is .. If i click YES why my program STOP??
    Last edited by Norm; 08-26-2011 at 04:42 PM. Reason: Changed to code tags

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Print out the value of n that is returned by the JOptionPane showConfirmDialog method to see it it is what you expect.
    Java Code:
    System.exit(0);[COLOR="red"]}[/COLOR]
    Hiding a closing } at the end of a statement makes it hard to read your code.
    Always put the closing } on a line by itself in vertical alignment with the line with the opening {
    Last edited by Norm; 08-26-2011 at 04:48 PM.

  6. #6
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    while(n==JOptionPane.YES_OPTION);
    word = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE);
    hmm... is this already ok norm??

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Use == to test for equality.
    Use = to assign a value.

  8. #8
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    what code ill use return?? or back again to
    word == JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE); ??

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    word == JOptionPane.showInputDia....
    Use = to assign a value to a variable.
    == is for comparing the values of variables.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Java Code:
    System.exit(0);[COLOR="red"][B]}[/B][/COLOR]
    Hiding a closing } at the end of a statement makes it hard to read your code.
    Always put the closing } on a line by itself in vertical alignment with the line with the opening {

  11. #11
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    can you give me a clue to back... please....... i just wanted to back the
    word = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE);
    for complete looping..

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Did you read my post#10?
    Did you fix your code for the problem described there?

  13. #13
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    yes... i already fix the post#10...
    my problem now is how to go back in DO statement is i click YES in JOptionPane.showConfirmDialog(null,"",JOptionPane. YES_NO_OPTION);

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    How did you fix what was wrong in post#10?
    Please post your new code because that fix should have helped you make your code work as you want it to.

  15. #15
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
     
    public class loop {
    	public static void main(string[] args){
     
    		int n;
    		string word="";
     
    		do{
    			word = joptionpane.showinputdialog(null,"enter a channel: ","ready",joptionpane.question_message);
    			n = integer.parseint(word);
     
    			if( n == 21 ){
    				joptionpane.showmessagedialog(null, "tv5","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 12 ){
    				joptionpane.showmessagedialog(null, "gma","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 2 ){
    				joptionpane.showmessagedialog(null, "studio 23","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 23 ){
    				joptionpane.showmessagedialog(null, "abc-cbn","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 10 ){
    				joptionpane.showmessagedialog(null, "ibc","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 5 ){
    				joptionpane.showmessagedialog(null, "rpn","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else if( n == 35 ){
    				joptionpane.showmessagedialog(null, "qtv","you're on the channel:",joptionpane.information_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			}
    			else{
    				joptionpane.showmessagedialog(null, "karat ang channel","ang channel kay",joptionpane.error_message);
    			n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    			if(n == joptionpane.no_option) 
    			system.exit(0);
    			}
    		}while(n==joptionpane.yes_option);
    		
    		
    	}
    }
    ..
    Already fix norm ... Thank you so much for all information about == and looping =)
    Last edited by Norm; 08-26-2011 at 05:19 PM. Reason: changed quote to code tag

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    It looks like you are asking the same question every time you get a matching answer.
    You should look at your logic for a way to only do it once.

  17. #17
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    yeah... ^_^ i've learn a lesson in you.. hahaha.. can i ask some program for Loop?? if you have can you give me ??
    i should study .. FOR LOOP with JOptionPane ^^

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    The construction of the loop and the code inside of the loop is not related to the usage of the JOptionPane class.
    You could mis-use the Scanner class in the same way.

  19. #19
    irnie1994 is offline Member
    Join Date
    Aug 2011
    Posts
    28
    Rep Power
    0

    Default

    what will be base for looping??
    and what are the difference for looping and do looping??

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

Page 1 of 2 12 LastLast

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 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
  •