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 6 of 6

Thread: Switch problem

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Switch problem

    Hey guys.

    I just had a problem with converting, and you guys helped me fixed it. So, here is my finished code:

    import java.util.Scanner;
     
    public class RandomKeyCode {
     
    	public static void generate() {
     
    		double c1, c2, c3, c4, c5;
    		String ccc1, ccc2, ccc3, ccc4, ccc5;
    		String[] choice = new String[5];
    		String keycode;
     
    		c1 = ((Math.random()*46)+1);
    		ccc1 = find(c1);
     
    		c2 = ((Math.random()*46)+1);
    		ccc2 = find(c2);
     
    		c3 = ((Math.random()*46)+1);
    		ccc3 = find(c3);
     
    		c4 = ((Math.random()*46)+1);
    		ccc4 = find(c4);
     
    		c5 = ((Math.random()*46)+1);
    		ccc5 = find(c5);
     
    		choice[0] = ccc1;
     
    		choice[1] = ccc2;
     
    		choice[2] = ccc3;
     
    		choice[3] = ccc4;
     
    		choice[4] = ccc5;
     
    		keycode = choice[0] + choice[1] + choice[2] + choice[3] + choice[4];
     
    		System.out.print("Your keycode is: " + keycode + ".");
    		System.out.print("Your numbers were " + c1 + " " + c2 + " " + c3 + " " + c4 + " " + c5);
     
    	}
     
    	public static void enter() {
     
     
     
    	}
     
    	public static String find(double c) {
     
    		String out = null;
     
    		switch((byte)c) {
     
    		case 1:
    			out = "a";
    		case 2:
    			out = "b";
    		case 3:
    			out = "c";
    		case 4:
    			out = "d";
    		case 5:
    			out = "e";
    		case 6:
    			out = "f";
    		case 7:
    			out = "g";
    		case 8: 
    			out = "h";
    		case 9:
    			out = "i";
    		case 10:
    			out = "j";
    		case 11:
    			out = "k";
    		case 12:
    			out = "l";
    		case 13:
    			out = "m";
    		case 14:
    			out = "n";
    		case 15:
    			out = "o";
    		case 16:
    			out = "p";
    		case 17:
    			out = "q";
    		case 18:
    			out = "r";
    		case 19:
    			out = "s";
    		case 20:
    			out = "t";
    		case 21:
    			out = "u";
    		case 22:
    			out = "v";
    		case 23: 
    			out = "w";
    		case 24:
    			out = "x";
    		case 25:
    			out = "y";
    		case 26:
    			out = "z";
    		case 27:
    			out = "1";
    		case 28:
    			out = "2";
    		case 29:
    			out = "3";
    		case 30:
    			out = "4";
    		case 31:
    			out = "5";
    		case 32:
    			out = "6";
    		case 33:
    			out = "7";
    		case 34:
    			out = "8";
    		case 35:
    			out = "9";
    		case 36:
    			out = "0";
    		case 37:
    			out = "!";
    		case 38:
    			out = "@";
    		case 39:
    			out = "#";
    		case 40:
    			out = "$";
    		case 41:
    			out = "%";
    		case 42:
    			out = "^";
    		case 43:
    			out = "&";
    		case 44:
    			out = "*";
    		case 45:
    			out = "(";
    		case 46:
    			out = ")";
     
    		}
     
    		return out;
     
    	}
     
    	public static void main(String[] args) {
     
    		String q = null; //question variable
     
    		Scanner scan = new Scanner(System.in);
     
    		System.out.print("Do you want to generate your key code, or do you want to enter it?");
    		q = scan.nextLine();
     
    		switch(q.toLowerCase()) {
     
    		case("generate") :
    			generate();
    		case("enter") :
    			enter();
     
    		}
     
    	}
     
    }

    So, I try to do it and here is the output:

    Do you want to generate your key code, or do you want to enter it?generate
    Your keycode is: ))))).Your numbers were 2.2230319103237237 40.409992358385196 46.01823508515554 8.75421673376253 7.440266533810639

    As you can see, it gives me random numbers, but since the switch can't handle the output of the decimal numbers, the switch defaults to the last character.

    I need to know how to make the switch able to sense the difference or how to switch the type so that it won't give me decimals.

    Help?

    -Silent


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Switch problem

    Can you explain more more detail exactly what you're trying to do, assume that we know nothing about your project. Note that trying to cast a double to a byte makes no sense at all.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Switch problem

    Trying to make a random key code generator.

  4. #4
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Switch problem

    For switch case, you need to use break and return, for instance:

    switch ((byte)c)
    {
         case 0:
              return 'a';
              break;
              /** more switch cases */
         default:
              return '*';
    }

    But using switch case in such a fashion shows a lack of understanding of programming principles in my opinion. You can eliminate all of that code by using a look up table, ie:

    public class characterGen
    {
        public static void main( String [] args)
        {
            byte [] character =
            {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '#', 
                '$', '%', '^', '&', '*', '(', ')', 0
            };
            int i=0;
            while (character[i]!=0)
            {
                System.out.format("%c",character[i]);
                i++;
            }
        }
    }

    So, if you had that array of bytes available to a method, you could return one, like:

    public class characterGen
    {
        public static byte getChar( byte x )
        {
            if( x<0 || x>45 )
            {
                x = x%46; // Reduces the number to 45 or less I think...
            }
            byte [] character =
            {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '#', 
                '$', '%', '^', '&', '*', '(', ')'
            };
            return (byte)character[x];
        }
    }
    Regards,

    Shaun.
    Last edited by ShaunB; October 31st, 2012 at 05:22 PM. Reason: Called a method a routine, I get confused between routines, methods and functions.

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Switch problem

    So if I were to use one of the things you mentioned (which I only partially understand, since I'm not that good at Java) how would I structure it in my code?

  6. #6
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Switch problem

    Hi SilentNite17,

    I missed your reply and I guess that it's moot now, but those who're interested, a quick and dirty example of using a look-up table to generate a random key has been posted on my PasteBin account here.

    The example could be re-factored a bit as there is some redundant code in there, but I'm sure that won't take anyone too long. Also, it should be easy enough to implement in another language, such as C or C++.

    Regards,

    Shaun.

Similar Threads

  1. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  2. Switch Case Problem help
    By Kikiam in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 18th, 2012, 06:11 PM
  3. Switch problem. How do I return to my menu?
    By jonny007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2012, 12:54 PM
  4. Credit Card Problem (While and Switch will be used)
    By odun in forum Object Oriented Programming
    Replies: 3
    Last Post: March 29th, 2011, 12:54 AM
  5. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM