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.

Recommended Answers

All 2 Replies

LineWriter Menue Hexadecimal WHERE this..

post all necessary class which you need do debug this....

i can't run this ...

my bad...I did not realize that...here are the other ones, thanks

public class Menue 
{
	public void Display()
	{
		System.out.println("Select one of the following options:");
		System.out.println("");
		System.out.println("1. Convert Decimal to Binary");
		System.out.println("2. Convert Decimal to Hexadecimal");
		System.out.println("3. Convert Binary to Decimal");
		System.out.println("4. Convert Binary to Hexadecimal");
		System.out.println("5. Convert Hexadecimal to Binary");
		System.out.println("6. Convert Hexadecimal to Decimal");
		System.out.println("7. End program");
		System.out.println("");
	}
}

import java.lang.String;

public class Decimal 
{
	int number;
	String numb = "";
	
	//assigned the entered number X to the variable number
	public void getNumber(int x)
	{
		number = x;
	}
	
	//convert number into binary
	public void toBin()
	{
		numb = "";
		
		do
		{
			numb = Integer.toString(number%2) + numb;
			number = number/2;
			
		}
		while (number > 0);
		
		for (int n = numb.length(); n < 8; n++)
		{
			numb = '0' + numb;
		}
	}
	
	//convert number into hexadecimal
	public void toHex()
	{
		
		int r;
		numb = "";
			
		do
		{
			r = number%16;
			
			if (r == 10)
			{
				numb = 'A' + numb;
			}
			else if  (r == 11)
			{
				numb = 'B' + numb;
			}
			else if  (r == 12)
			{
				numb = 'C' + numb;
			}
			else if  (r == 13)
			{
				numb = 'D' + numb;
			}
			else if  (r == 14)
			{
				numb = 'E' + numb;
			}
			else if  (r == 15)
			{
				numb = 'F' + numb;
			}
			else
			{
				numb = Integer.toString(number%16) + numb;
			}
			
			number = number/16;
			
		}
		while (number > 0);
		
		for (int n = numb.length(); n < 4; n++)
		{
			numb = '0' + numb;
		}
	}
	
	//return converted number
	public String outConv()
	{
		return numb;
	}
}

import java.lang.String;

public class Hexadecimal
{
	String hex = "";
	String part = "";
	
	//array to map byts from hexadecimal values
	String[] Bin_Data = new String[16];
	char[] Hex_Data = new char[16];
	double[] Dec_Data = new double[16];
	
	public Hexadecimal()
	{
		Bin_Data[0] = "0000";
		Bin_Data[1] = "0001";
		Bin_Data[2] = "0010";
		Bin_Data[3] = "0011";
		Bin_Data[4] = "0100";
		Bin_Data[5] = "0101";
		Bin_Data[6] = "0110";
		Bin_Data[7] = "0111";
		Bin_Data[8] = "1000";
		Bin_Data[9] = "1001";
		Bin_Data[10] = "1010";
		Bin_Data[11] = "1011";
		Bin_Data[12] = "1100";
		Bin_Data[13] = "1101";
		Bin_Data[14] = "1110";
		Bin_Data[15] = "1111";
		
		Hex_Data[0] = '0';
		Hex_Data[1] = '1';
		Hex_Data[2] = '2';
		Hex_Data[3] = '3';
		Hex_Data[4] = '4';
		Hex_Data[5] = '5';
		Hex_Data[6] = '6';
		Hex_Data[7] = '7';
		Hex_Data[8] = '8';
		Hex_Data[9] = '9';
		Hex_Data[10] = 'A';
		Hex_Data[11] = 'B';
		Hex_Data[12] = 'C';
		Hex_Data[13] = 'D';
		Hex_Data[14] = 'E';
		Hex_Data[15] = 'F';
		
		for (int n = 0; n < 16; n++)
		{
			Dec_Data[n] = n;			
		}
	}
	
	//assign input "x" to variable "bin"
	public void getNumber(String x)
	{
		hex = x;
	}
	
	//convert input to decimal and assign it to variable "bin"
	public void toDec()
	{
		double conv = 0.0;
		int n = 0;
		
		//loop through the hex number
		for (int i = hex.length(); i > 0; i--)
		{
			int c = 0;
			
			//compare each character with the Hex_Data array
			while (hex.charAt(i - 1) != Hex_Data[c])
			{
				c++;
			}
			
			conv = Dec_Data[c]*Math.pow(16,n) + conv;
			n++;
		}
		
		int part = (int) conv;
		
		hex = Integer.toString(part);
	}
	
	public void toBin()
	{
		String conv = "";
		
		for (int i = hex.length(); i > 0; i--)
		{
			int c = 0;
			
			while (hex.charAt(i - 1) != Hex_Data[c])
			{
				c++;
			}
			
			conv = Bin_Data[c] + conv;
		}
		
		hex = conv;
	}
	
	//output conversion
	public String outConv()
	{
		return hex;
	}
}

// LineWriter.java

import java.io.*;

/* @author Richard Stegman
 * @version 2.1, 1/5/07
 * 
 * LineWriter provides simple methods for opening and writing to text files.
 * 
 * Example:
 * 
 *      LineWriter lw = new LineWriter(foo.txt");
 *      lw.println("This is a string.");
 *      lw.println();
 *      lw.println(108);
 *      lw.close(); 
 *
 */

public class LineWriter {
    private PrintWriter outFile;
    
    /*
     * LineWriter constructor. 
     * Creates a new file or overwrites an existing file.
     * @param fileName name of the file to be created
     */
    
    public LineWriter(String fileName) {
        try {
            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);
            outFile = new PrintWriter(bw);
        }    
        catch (IOException e) {
            System.out.println("LineWriter cannot open output file: " + fileName);
            e.printStackTrace();
        }
    }

    /*
     * LineWriter constructor. 
     * Creates a new file. If the file exists can append to it.
     * @param fileName name of the file to be created
     * @param mode if equal to "a" opens in append mode
     */
    
    public LineWriter(String fileName, String mode) {
        try {
            FileWriter fw = new FileWriter(fileName, "a".equals(mode));
            BufferedWriter bw = new BufferedWriter(fw);
            outFile = new PrintWriter(bw);
        }    
        catch (IOException e) {
            System.out.println("LineWriter cannot open output file: " + fileName);
            e.printStackTrace();
        }
    }

    /**
     * Outputs a string and newline to the file.
     * @param s string to be output
     */
     
    public void println(String s) {
        outFile.println(s);
    }

    /**
     * Outputs a string to the file.
     * @param s string to be output
     */
     
    public void print(String s) {
        outFile.print(s);
    }

    /**
     * Outputs an integer and newline to the file.
     * @param i integer to be output
     */
     
    public void println(int i) {
        outFile.println(i);
    }

    /**
     * Outputs an integer to the file.
     * @param i integer to be output
     */
     
    public void print(int i) {
        outFile.print(i);
    }

    /**
     * Outputs a long and newline to the file.
     * @param l long to be output
     */
     
    public void println(long l) {
        outFile.println(l);
    }

    /**
     * Outputs a long to the file.
     * @param l long to be output
     */
     
    public void print(long l) {
        outFile.print(l);
    }

     /**
     * Outputs a short and newline to the file.
     * @param s short to be output
     */
     
    public void println(short s) {
        outFile.println(s);
    }

    /**
     * Outputs a short to the file.
     * @param s short to be output
     */
     
    public void print(short s) {
        outFile.print(s);
    }
    
    /**
     * Outputs a byte and newline to the file.
     * @param b byte to be output
     */
     
    public void println(byte b) {
        outFile.println(b);
    }

    /**
     * Outputs a byte to the file.
     * @param b byte to be output
     */
     
    public void print(byte b) {
        outFile.print(b);
    }
    
    /**
     * Outputs a boolean and newline to the file.
     * @param b boolean to be output
     */
     
    public void println(boolean b) {
        outFile.println(b);
    }

    /**
     * Outputs a boolean to the file.
     * @param b boolean to be output
     */
     
    public void print(boolean b) {
        outFile.print(b);
    }

    /**
     * Outputs a float and newline to the file.
     * @param f float to be output
     */
     
    public void println(float f) {
        outFile.println(f);
    }

    /**
     * Outputs a float to the file.
     * @param f float to be output
     */
     
    public void print(float f) {
        outFile.print(f);
    }

    /**
     * Outputs a double and newline to the file.
     * @param d double to be output
     */
     
    public void println(double d) {
        outFile.println(d);
    }

    /**
     * Outputs a double to the file.
     * @param d double to be output
     */
     
    public void print(double d) {
        outFile.print(d);
    }

    /**
     * Outputs a character and newline to the file.
     * @param c char to be output
     */
     
    public void println(char c) {
        outFile.println(c);
    }

    /**
     * Outputs a character to the file.
     * @param c char to be output
     */
     
    public void print(char c) {
        outFile.print(c);
    }

    /**
     * Outputs an object and newline to the file.
     * @param o object to be output
     */
     
    public void println(Object o) {
        outFile.println(o);
    }

    /**
     * Outputs an object to the file.
     * @param o object to be output
     */
     
    public void print(Object o) {
        outFile.print(o);
    }
    
    /**
     * Outputs a newline character to the file.
     */
     
    public void println() {
        outFile.println();
    }

    /**
     * Closes the file.
     */
     
    public void close() {
        outFile.flush();
        outFile.close();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.