hi, i'm having problems with the output. I do not get anything when I select 4 and enter a binary number. Here is my code, if anyone could point out what i'm doing worng, thanks.
import java.util.Scanner;
public class Driver
{
public static void main(String [] arg)
{
LineWriter Lw = new LineWriter("csis.txt");
Menue Mn = new Menue();
Decimal Dec = new Decimal();
Binary Bin = new Binary();
Hexadecimal Hex = new Hexadecimal();
Scanner Scan = new Scanner(System.in);
int selection = 0;
boolean select = true;
String convertion = "";
do
{
Mn.Display();
//Loop to check that the correct option was selected
do
{
System.out.print("Enter a selection from the munue: ");
selection = Scan.nextInt();
System.out.println("");
if ((selection > 7) || (selection < 1))
{
select = false;
}
else
{
select = true;
}
}
while (select != true);
//if selection is different than 7 execute; otherwise, end program
if (selection != 7)
{
if (selection == 1)
{
System.out.print("Enter decimal number: ");
int x = Scan.nextInt();
Dec.getNumber(x);
Dec.toBin();
convertion = Dec.outConv();
}
else if (selection == 2)
{
System.out.print("Enter decimal number: ");
int x = Scan.nextInt();
Dec.getNumber(x);
Dec.toHex();
convertion = Dec.outConv();
}
else if (selection == 3)
{
System.out.print("Enter binary number: ");
String x = Scan.next();
Bin.getNumber(x);
Bin.toDec();
convertion = Bin.outConv();
}
else if (selection == 4)
{
System.out.print("Enter binary number: ");
String x = Scan.next();
Bin.getNumber(x);
Bin.toHex();
convertion = Bin.outConv();
}
else if (selection == 5)
{
System.out.print("Enter hexadecimal number: ");
String x = Scan.next();
Hex.getNumber(x);
Hex.toBin();
convertion = Hex.outConv();
}
else if (selection == 6)
{
System.out.print("Enter hexadecimal number: ");
String x = Scan.next();
Hex.getNumber(x);
Hex.toDec();
convertion = Hex.outConv();
}
System.out.println();
System.out.println("Number equals: " + convertion);
System.out.println();
}
else
{
System.out.println();
System.out.println("Program terminated.");
System.out.println();
}
}
while (selection != 7);
Lw.close();
}
}
import java.lang.String;
public class Binary
{
String bin;
//array to map byts to hexadecimal values
String[][] dataConv = new String[16][2];
public Binary()
{
dataConv[0][0] = "0000";
dataConv[1][0] = "0001";
dataConv[2][0] = "0010";
dataConv[3][0] = "0011";
dataConv[4][0] = "0100";
dataConv[5][0] = "0101";
dataConv[6][0] = "0110";
dataConv[7][0] = "0111";
dataConv[8][0] = "1000";
dataConv[9][0] = "1001";
dataConv[10][0] = "1010";
dataConv[11][0] = "1011";
dataConv[12][0] = "1100";
dataConv[13][0] = "1101";
dataConv[14][0] = "1110";
dataConv[15][0] = "1111";
dataConv[0][1] = "0";
dataConv[1][1] = "1";
dataConv[2][1] = "2";
dataConv[3][1] = "3";
dataConv[4][1] = "4";
dataConv[5][1] = "5";
dataConv[6][1] = "6";
dataConv[7][1] = "7";
dataConv[8][1] = "8";
dataConv[9][1] = "9";
dataConv[10][1] = "A";
dataConv[11][1] = "B";
dataConv[12][1] = "C";
dataConv[13][1] = "D";
dataConv[14][1] = "E";
dataConv[15][1] = "F";
}
//assign input "x" to variable "bin"
public void getNumber(String x)
{
bin = x;
}
//convert input to decimal and assign it to variable "bin"
public void toDec()
{
double convertion = 0;
double n = 0;
for (int i = bin.length(); i > 0; i--)
{
int nm = Integer.parseInt(Character.toString(bin.charAt(i - 1)));
convertion = Math.pow(2,n)*(nm) + convertion;
n++;
}
bin = String.valueOf((int)convertion);
}
//convert input to decimal and assign it to variable "bin"
public void toHex()
{
//variable holds the final string to pass onto variable bin
String conv = "";
//variable part holds 4 characters at a time
String part = "";
//make length of binary number a multiple of 4
while (bin.length()%4 != 0)
{
bin = "0" + bin;
}
for (int i = bin.length(); i > 0; i--)
{
part = bin.charAt(i - 1) + part;
//from right to left, once the first 4 digits have been saved, check on the array
if (part.length() == 4)
{
//n = 0 for search to begin at [0][1]
int n = 0;
while (part != dataConv[n][0]);
{
n++;
}
//passing data from [n][1]
conv = dataConv[n][1] + conv;
}
part = "";
}
bin = conv;
}
//output conversion
public String outConv()
{
return bin;
}
}
thanks.