Quote:
Originally Posted by zverys36
OK, so I have to read a few numbers using Java. The numbers are not hard-coded - they depend on whichever text file I choose to read. I need to take those numbers that I read and translate them into binary. For example, if a text file contains the numbers:
I want the numbers to be read and translated into binary (skipping the string).
PHP Code:
10
a string
101
|
Gotcha.
The easiest way would be to read the data in as a string, parse it as an integer and display it. Using a try/catch would suit for this.
PHP Code:
BufferedReader br;
try
{
br = new BufferedReader(new Filereader("yourfile.txt"));
String sLine = null;
while (null != (sLine = br.readLine()))
{
String sDisplay = "";
try
{
sDisplay = Integer.toBinaryString(Integer.parseInt(sLine).intValue());
}
catch (NumberFormatException ex)
{
sDisplay = sLine;
}
System.out.println("Line: " + sDisplay);
}
}
catch (IOException ex)
{
System.out.println("Failed to open file.");
}
finally
{
try
{
br.close();
}
catch (Exception ex)
{
}
}
Untested, but something like that should work.