Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Switch

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch

    hello, I have a problem with my code, I wrote a switch condition, and I just wanted to test it for the first case, but when I type 1, there is not output while I put one in my case 1 code. Also when I write 6, I get Thank you as output but also the ouput of the case 1... Here is my code
     public static void main(String[] args) {
                Scanner input=new Scanner(System.in);
     
                int choice;
               do{
                System.out.println("Welcome to Amazon Application!");
                System.out.println("What do you want to do?\n");
                System.out.println("1-Register\n2-Modify profil and personnal info\n3-Search books\n4-Create a shopping cart\n5-Submit an order\n6-Exit");
                System.out.println("Please enter your choice:");
                choice=input.nextInt();  
                switch(choice/6){
                    case 1: System.out.println("Case 1 test");
                        break;
                     default:
                            break;
                }
               }while(choice!=6);
               System.out.println("Thank you!");
            }
    }


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    your switch has (choice/6)
    I think what you want is just switch(choice)
    remember the / will return whole numbers not remainder
    so when u enter 6....6/6 = 1

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    No I removed it but it still does not output anything for 1 etc except for 6
    here is the code:
    public static void main(String[] args) {
                Scanner input=new Scanner(System.in);
     
                int choice;
               do{
                System.out.println("Welcome to Amazon Application!");
                System.out.println("What do you want to do?\n");
                System.out.println("1-Register\n2-Modify profil and personnal info\n3-Search books\n4-Create a shopping cart\n5-Submit an order\n6-Exit");
                System.out.println("Please enter your choice:");
                choice=input.nextInt();  
                switch(choice){
                    case 1: System.out.println("Case 1 test");
                        break;
                    case 2: System.out.println("Case 2 test");
                    case 3: System.out.println("Case 3 test");
                        break;
                    case 4: System.out.println("Case 4 test");
                        break;
                    case 5:  System.out.println("Case 5 test");
                        break;
                    case 6: System.out.println("Byebye!");
                        break;
     
                default:  System.out.println("Wrong choice!");
                            break;
                }
               }while(choice!=6);
     
            }

    When i write 1,2,3,4 or 5 there is NO output, but when I write 6, evrything is fine, I have the ouput ByeBye and the program stops

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    hummm......it works for me
    can u please show the class declaration and imports your using?

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch

     
    package  amazonapplication;
    import java.util.Scanner;
    /**
     *
     * @author
     */
        public class AmazonApplication {
     
     
     
     
            public static void main(String[] args) {
                Scanner input=new Scanner(System.in);
     
                int choice;
               do{
                System.out.println("Welcome to Amazon Application!");
                System.out.println("What do you want to do?\n");
                System.out.println("1-Register\n2-Modify profil and personnal info\n3-Search books\n4-Create a shopping cart\n5-Submit an order\n6-Exit");
                System.out.println("Please enter your choice:");
                choice=input.nextInt();  
                switch(choice){
                    case 1: System.out.println("Case 1 test");
                        break;
                    case 2: System.out.println("Case 2 test");
                    case 3: System.out.println("Case 3 test");
                        break;
                    case 4: System.out.println("Case 4 test");
                        break;
                    case 5:  System.out.println("Case 5 test");
                        break;
                    case 6: System.out.println("Byebye!");
                        break;
     
                default:  System.out.println("Wrong choice!");
                            break;
                }
               }while(choice!=6);
     
            }
    }

    Here is the main class, but this a project so there a lot of other classes, but I guess written like this, it should work normally, but it works only for the case 6...

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    I think u have included all relevant code.
    the only thing I see really different is I did import java.util.*;
    try that.


    heres my "equivalent" code.
    import java.io.*;
    import java.util.*;
     
    public class a1
    {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
     
        int choice;
        do{
            System.out.println("Welcome to Amazon Application!");
            System.out.println("What do you want to do?\n");
            System.out.println("1-Register\n2-Modify profil and personnal info\n3-Search books\n4-Create a shopping cart\n5\
    -Submit an order\n6-Exit");
            System.out.println("Please enter your choice:");
            choice=input.nextInt();
            switch(choice){
            case 1: System.out.println("Case 1 test");
                break;
            case 2: System.out.println("Case 2 test");
            case 3: System.out.println("Case 3 test");
                break;
            case 4: System.out.println("Case 4 test");
                break;
            case 5:  System.out.println("Case 5 test");
                break;
            case 6: System.out.println("Byebye!");
                break;
     
            default:  System.out.println("Wrong choice!");
                break;
            }
        }while(choice!=6);
     
    }
    }

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    Tried it but nothing, I don't understand, everything is fine but there is a problem, It should'nt have a relation with the other classes no ?

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    Why isn't it coloring your code I wonder?
    I know sometimes if emacs colors code funny, it CAN mean something is up with the code.
    But that does not make sense because it works for me.
    Im sorry, Im stumped. Did you copy my code to your computer and run it there?

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    When i copy your code, I get an error in the line of println("1- etc....)
    Well I am running out of time, I have to submit it in 1h so I will just do something... don't get everything is fine and this switch does not work.. that's what I hate with coding, errors that happens for you but with others it's fine, and you can't figure it ou ...

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    sorry, wish I could do more for you.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch

    No problem, thx anyways!

    --- Update ---

    Please can you help with this ?
     public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        Member member1=new Member("toto","totopass");
        Member member2= new Member("toto2","totopass2");
        Member member3=new Member("Issam","Issampass");
     
        MemberList members=new MemberList();
     
        members.addmember(member1);
        members.addmember(member2);
        members.addmember(member3);
        System.out.println("*****************Printing the list of current members********************");
        System.out.println(members.toString());
        System.out.println("*****************Register as a member************************************");
        System.out.println("Enter the username:");
        String username="";
        username=input.nextLine();
        System.out.println("Enter the password:");
        String password="";
        password=input.nextLine();
        Member member4=new Member("username","password");
        members.addmember(member4);
        System.out.println("*****************Printing the list of current members********************");
        System.out.println(members.toString());

    When i ask the user the enter a username and a password with the scanner, and then I create a new member with those username and password, but I print it with toString, I have the output username and password instead of the string the user entered ... pls help!!

    --- Update ---

    I am sorry nevermind, I found my mistake, it should be Member member4=new Member(username,password) instead of Member member4=new Member("username","password");

Similar Threads

  1. IF and SWITCH Statements: How and When to Use Them
    By snowguy13 in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 11th, 2012, 10:46 AM
  2. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  3. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. Why Did You switch to Java?
    By Sisyphus in forum The Cafe
    Replies: 2
    Last Post: May 26th, 2010, 04:01 AM