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

Thread: Converting?

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

    Default Converting?

    So, I'm making a key code generator that creates a five character key code using a-z and 0-9 and )-!. That means there are 46 possible characters for each piece of keycode.

    Here is my code so far:

    import java.util.Random;
    import java.util.Scanner;
    import java.io.*;
     
    public class RandomKeyCode {
     
    	public static String generate() {
     
    		double c1, c2, c3, c4, c5;
    		String cc1, cc2, cc3, cc4, cc5;
    		String[] choice = new String[5];
    		String num;
    		Random rand = new Random();
     
    		c1 = ((Math.random()*46)+1);
    		find(c1);
     
    		c2 = ((Math.random()*46)+1);
    		find(c2);
     
    		c3 = ((Math.random()*46)+1);
    		find(c3);
     
    		c4 = ((Math.random()*46)+1);
    		find(c4);
     
    		c5 = ((Math.random()*46)+1);
    		find(c5);
     
     
     
    	}
     
    	public static void enter() {
     
     
     
    	}
     
    	public static String find(double c) {
     
    		String out;
     
    		switch(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; //question variable
     
    		System.out.print("Do you want to generate your key code, or do you want to enter it?");
     
    		switch(q.toLowerCase()) {
     
    		case("generate") :
    			generate();
    		case("enter") :
    			enter();
     
    		}
     
    	}
     
    }

    The errors I am concerned with are the converting errors. In the generate() part of the code, the


    double c1, c2, c3, c4, c5;

    shows that there are a couple of double numbers that correspond to the parts of the keycode. In the later part of the script, with the switch(c), it says it can't switch on a variable type double.

    That's only one of my problems.

    When I try to switch the double values to ints, the line

    c1 = ((Math.random()*46)+1);
    find(c1);

    tells me that it can't convert from an int to a double to do math. Also, when I try to do it with floats, same thing happens. I also try to add .toInt() or .toString() to convert it to something, it says it can't convert with the primitive type double.

    AGH!

    Help please?

    -Silent


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

    Default Re: Converting?

    Hello? Help anyone?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Converting?

    The double 1.0 does not equal the int 1 which you are trrying to use in the switch statement. The solution is to do as you attempted and change the doubles to ints but then you encounter the other problem if cannot convert double to int. Well you can if you cast the double value to an int value. If you do not know what I mean by cast then you need to research it.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting?

    First of all, you can't switch doubles. In Java, you can only switch whole numbers (i.e. int). When trying to switch a double or a float or even a String you'll get errors. You can solve this by either changing your doubles into ints, but that might mess up the rest of your program. You can also cast int to double, which basically means you can convert that decimal number into a whole number. Note that int ALWAYS rounds down, so even if you have a decimal like 1.999999999, when you cast to int, you'll get 1. You can do it this way:

    switch((int)c) { //the "(int)" before the double variable will convert it to a whole number
     
    case 1: 
    break;
    case 2: 
    break;
    case 3: 
    break; 
    //etc
     
    }

    Using toInt() or toString() on a primitive type (int, double, float, byte, etc) is impossible, since they aren't objects. Again, if you want to convert a decimal (float, double) to an int, you'll have to cast.

Similar Threads

  1. Converting Name.
    By LoganC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 08:09 AM
  2. Converting Cases Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 27th, 2010, 12:07 PM
  3. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 06:09 PM
  4. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 09:58 AM
  5. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM