Data conversion

To test myself, I set myself the task of writing a process to convert an input String from one format to another. For example, allowed String formats are, integer, hexadecimal, octal or binary. You can then choose to convert to another type (of the same value above).

Is there any improvements to the code that you guys and/or girls can suggest?

import java.io.*;

// This receives a user input to define the type of information to convert and the type of

// information to convert to.

publicclass ConvertData{

publicstaticvoid main(String args[])throws IOException{

char inpChoice =new Character(InputChoice("Input"));

char outChoice =new Character(InputChoice("Output"));

// Receive VALUE of data to convert

System.out.print("Please enter the VALUE of the data to be converted\n");

BufferedReader inReader =new BufferedReader(new InputStreamReader(System.in));

String inputString = inReader.readLine();

// Now I need to do the conversion based on the initial value in inputString, and the

// conversion factors in inpChoice and outChoice

try{

//System.out.println("After converting the input string into an Integer field, the value is: " + GetInt(inpChoice, inputString));

System.out.println("After converting the integer field to your desired format the output is: " + ConvertTarget(GetInt(inpChoice, inputString),outChoice));

}

catch (NumberFormatException e){

System.out.println("Error a number format exception has occurred!");

}

}

staticvoid ClearInputBuffer()throws IOException{

// Clear out input buffer

char a;

do{

a = (char) System.in.read();

}while (a !='\n');

}

staticchar InputChoice(String s)throws IOException{

char a;

// Receive INPUT/OUTPUT type of data conversion

System.out.print("Please enter the " + s +" type of data..\n");

System.out.println("(i) = integer, (b) = binary, (h) = hexadecimal, (o) = octal");

do{

a = (char) System.in.read();

}while(a !='i' & a !='b' & a !='h' & a !='o');

ClearInputBuffer();

return a;

}

static String ConvertTarget(int a,char c){

String returnString;

if (c =='b') returnString = Integer.toBinaryString(a);// Convert to binary String

else

if (c =='h') returnString = Integer.toHexString(a);// Convert to hexadecimal String

else

if (c =='i') returnString = Integer.toString(a);// Convert to integer String

else

if (c =='o') returnString = Integer.toOctalString(a);// Convert to octal String

else

returnString =new String("Error in conversion");

return returnString;

}

staticint GetInt(char c, String s)throws NumberFormatException{

// Get the integer value of a String field passed in which may be either

// binary, octal, integer or hexadecimal

int base = 16;// Default to Hexadecimal

if (c =='b') base = 2;// if binary

if (c =='o') base = 8;// if octal

if (c =='i') base = 10;// if integer

int inpValue = Integer.parseInt(new String(s), base);

return inpValue;

}

}

[6691 byte] By [hockeygod76a] at [2007-11-27 5:16:11]
# 1
Yes, don't work with charactors - reduce everything to bytes.
watertownjordana at 2007-7-12 10:38:43 > top of Java-index,Java Essentials,New To Java...