Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
Like Tree1Likes

Thread: How to loop this????

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

    Default

    switch (n) {
    case 1:
    JOptionPane.showMessageDialog(null, "You Pick GMA Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    norm how to Loop this?? if ill get the break; it will read the next case ..

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What part of what you posted do you want to put inside of a loop?

    The syntax for a loop:

    begin the loop statement

    ... The code inside of the loop

    end loop statement

    The begin and end parts are different for each type of loop
    Last edited by Norm; 08-26-2011 at 07:12 PM.

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

    Default

    for(;;){
    ans = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE);
    n = Integer.parseInt(ans);
    switch (n) {
    case 1:
    JOptionPane.showMessageDialog(null, "You Pick GMA Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 2:
    JOptionPane.showMessageDialog(null, "You Pick ABS Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    ... after i put a number 1 it will be back the ans = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE); and choose again a case...

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What is your question?

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

    Default

    Java Code:
    import javax.swing.JOptionPane;
    public class INPUT {
    public static void main(String[] args) {
    int n;
    String ans;
    for(;;){
    	ans = JOptionPane.showInputDialog(null,"Enter A Channel: ","READY",JOptionPane.QUESTION_MESSAGE);
    			n = Integer.parseInt(ans);
    switch (n) {
    case 12: 
    JOptionPane.showMessageDialog(null, "You Pick GMA Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 2:
    JOptionPane.showMessageDialog(null, "You Pick ABS Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 4:
    JOptionPane.showMessageDialog(null, "You Pick Kabarkada Network ","Channel",JOptionPane.INFORMATION_MESSAGE ); 
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 21: 
    JOptionPane.showMessageDialog(null, "You Pick Tv 5 ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break; 
    case 27: 
    JOptionPane.showMessageDialog(null, "You Pick China Channel","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 10: 
    JOptionPane.showMessageDialog(null, "You Pick IBN ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 5: 
    JOptionPane.showMessageDialog(null, "You Pick Solar Sports ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 39: 
    JOptionPane.showMessageDialog(null, "You Coc Phinma Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break;
    case 35: 
    JOptionPane.showMessageDialog(null, "You Pick QTV " );
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    break; 
    default:
    //... If we get here, something is wrong. Defensive programming.
    JOptionPane.showMessageDialog(null, "Try Another Channel OR AYUHA IMUNG ANTENNA ","An Error Occur ",JOptionPane.WARNING_MESSAGE);
    n=JOptionPane.showConfirmDialog(null, "WHAT TELEVISION CHANNEL?","CHOOSE A CHANNEL",JOptionPane.YES_NO_OPTION);
    
    }while(n==JOptionPane.NO_OPTION);
    System.exit(0);
    }
    }
    }
    this code... if you put a case number ... after choosing a case number the messagedialog will appear .. after that
    i want to back the Inputdialog but it will exit because of the break; how to avoid that?
    Last edited by Norm; 08-26-2011 at 07:20 PM. Reason: change quote to code

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    it will exit because of the break
    Which break? What exits? The switch statement of the for loop?
    I see many break statements in your code.

    Your {} are messed up again.
    Also you need to properly indent your code to show nesting within the {}s

    There is confusion in your code about using a for loop, a switch and a while loop.
    Where do each of them begin and where do each end.
    Last edited by Norm; 08-26-2011 at 07:24 PM.

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

    Default

    yeah it will exit because of the break... it is possible continue looping with out break;??
    if ill get the break; the
    JOptionPane.showMessageDialog(null, "You Pick GMA Network ","Channel",JOptionPane.INFORMATION_MESSAGE );
    and other JOptionPane.showMessageDialog will be show...

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Your {} are messed up again.
    Also you need to properly indent your code to show nesting within the {}s

    Put a comment on each of the ending } that ends a for loop and that ends a switch:
    Java Code:
       for( ) { 
    ...
       } [COLOR="red"]// end for()[/COLOR]
       switch() {
     ...
       }[COLOR="red"] // end switch[/COLOR]
    There is confusion in your code about using a for loop, a switch and a while loop.
    Where do each of them begin and where do each end.

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

    Default Here is the continues norm

    for(;;){
    ans = joptionpane.showinputdialog(null,"enter a channel: ","ready",joptionpane.question_message);
    n = integer.parseint(ans);
    switch (n) {
    case 21:
    Joptionpane.showmessagedialog(null, "you pick tv 5 ","channel",joptionpane.information_message );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    case 27:
    Joptionpane.showmessagedialog(null, "you pick china channel","channel",joptionpane.information_message );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    case 10:
    Joptionpane.showmessagedialog(null, "you pick ibn ","channel",joptionpane.information_message );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    case 5:
    Joptionpane.showmessagedialog(null, "you pick solar sports ","channel",joptionpane.information_message );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    case 39:
    Joptionpane.showmessagedialog(null, "you coc phinma network ","channel",joptionpane.information_message );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    case 35:
    Joptionpane.showmessagedialog(null, "you pick qtv " );
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);
    break;
    default:
    //... If we get here, something is wrong. Defensive programming.
    Joptionpane.showmessagedialog(null, "try another channel or ayuha imung antenna ","an error occur ",joptionpane.warning_message);
    n=joptionpane.showconfirmdialog(null, "what television channel?","choose a channel",joptionpane.yes_no_option);

    }//... End switch
    while(n==joptionpane.no_option);
    system.exit(0);
    }//... End for
    }
    }
    here.. ...

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Also you need to properly indent your code to show nesting within the {}s

    What is inside of the while() loop?
    When is the System.exit(0) code is executed?

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

    Default

    if default was activate and the confirmationdialog will ask if you change channel YES or NO.. if NO it will be System.exit(0)... If yes Continue Choosing a Case NUmber..

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    That is what you WANT to happen.

    Look at your code and see what WILL happen when it is executed.

    Think about these questions again:

    What code is inside of the while() loop?

    When is the System.exit(0) code executed?

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

    Default

    my concern is how about if i choose a right case ... how to continue it??

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    how to continue it??
    Correct your code. I've asked you to look at your code and describe here what it is doing.

    Think about these questions again:

    What code is inside of the while() loop?

    When is the System.exit(0) code executed?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Here is a hint.
    This is what your code is doing:
    begin for loop
    ask question and get response
    enter switch statement
    do case for user input
    show message
    get user input
    break from switch statement
    enter and exit while loop
    System exit

    You never get to the end of the for loop because of the exit statement

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

Page 2 of 2 FirstFirst 12

Similar Threads

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