Reputation: 1
Converting letters (A-F and any others should say error) into binary using a switch case this is the main:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();
keyboard.getBinary();
keyboard.toString();
and the class:
import static java.lang.System.*;
public class HexToBinary
{
private char hex;
public HexToBinary()
{
hex=0;
}
public HexToBinary(char hexNum)
{
hex=hexNum;
}
public void setHex(char hexNum)
{
hex=hexNum;
}
public String getBinary()
{
String letter = letter;
switch(letter)
{
case A: letter = 1010;
case B: letter = 1011;
case C: letter = 1100;
case D: letter = 1101;
case E: letter = 1110;
case F: letter = 1111;
case default: letter = ERROR;
}
return "";
}
public String toString()
{
System.out.println(letter+" is " getBinary + "in binary!")
return "";
}
the error says it cannot find symbol at the period of "keyboard.getBinary();" not sure what the problem is
Upvotes: 0
Views: 788
Reputation: 285405
the error says it cannot find symbol at the period of "keyboard.getBinary();" not sure what the problem is
Scanner doesn't have a getBinary()
method as the API will tell you: Scanner API.
More importantly
But your HexToBinary class does have this method. You will want to create a HexToBinary variable, assign it a HexToBinary object, and then call this method on this variable.
So not:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();
keyboard.getBinary();
keyboard.toString();
but rather:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a letter :: ");
String letter = keyboard.next();
// check that letter has only one char in it.
// convert your letter to a char.
// create a HexToBinary variable and object here
// use its methods to convert the char to binary.
Upvotes: 2