0

i need to make a switch statement that can use the appropriate conversion method. here is my code

public class ExerciseTwo
{
public static void main (Strings[] args)
{

Scanner input = new scanner(system.in);
String[] binary = { "0","1","2","3","4","5","6","7","8"};

for (c = 0; c < array.length; counter++)
binary[] = input.nextInt();

System.out.println("Enter number between 0 and 8");
number = input.nextInt();

system.out.printf("the number", "number_given", "is", "binaryVersion", "binary");
}
}
drum
  • 5,416
  • 7
  • 57
  • 91

2 Answers2

0

I'm sorry, but the description wasn't very clear to me. Are you simply trying to convert the input value (between 0 and 8) into a binary format (as in 2 -> 10, 7 -> 111) using a switch statement? If so, this code will work. If not, can you clarify the question for me?

Thanks!

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter number between 0 and 8"); 
    int number = input.nextInt();
    int binaryRepresentation = -1;

    switch (number)
    {
    case 0:
        binaryRepresentation = 0;
        break;
    case 1:
        binaryRepresentation = 1;
        break;
    case 2:
        binaryRepresentation = 10;
        break;
    case 3:
        binaryRepresentation = 11;
        break;
    case 4:
        binaryRepresentation = 100;
        break;
    case 5:
        binaryRepresentation = 101;
        break;
    case 6:
        binaryRepresentation = 110;
        break;
    case 7:
        binaryRepresentation = 111;
        break;
    case 8:
        binaryRepresentation = 1000;
        break;
    }

    System.out.printf("the number " + number + " is " + binaryRepresentation + " in binary (-1 means invalid input)"); 
}
tony
  • 88
  • 8
0

Do your home work yourself , Look at the http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html switch case definition. If you really want good solution for binary representation then look at API documentation of the Integer class http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html?is-external=true

Using the API doc is one of the first things you need to learn as a Java programmer.

RQube
  • 954
  • 3
  • 13
  • 28