This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Resilient Oracle PL/SQL: Building Resilient Database Solutions for Continuous Operation and have Stephen Morris on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

read content of text file then convert to decimal number

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning

I am very new to java, I am trying to read a binary value from a text file to assign it to a variable in java, to then convert it to its decimal number equivalent.

I can read a local text file using the code down below:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInput {

public static void main(String[] args) {

File file = new File("C:\\temp\\h12-2\\test.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

I can perform my calculation of binary to decimal using the code down below:

public class test2
{
public static void main(String args[])
{
String bin = "1111111111111111111111111";
double ctr=1, decimal=0;

for (double i = bin.length()-1; i>=0; i--)
{
if(bin.charAt((int) i)=='1')
decimal+=ctr;
ctr*=2;
}
System.out.println ("Decimal: "+decimal);
}
}


However I cannot succeed to put the two together successfully.
I'm thinking that it would have to be done with a variable in place of the ones String bin = "1111111111111111111111111";, I was thinking:

String Convert;
Convert = (dis.readLine());
String bin = "Convert";

It didn't work, my decimal result was 0.0 instead of obtaining the conversion of the binary numbers contained in the text file.

So I thought that perhaps I was using the wrong type and changed my String to an "int" but I receive an error message in netbeans telling me "incompatible types
Found: java.lang.string
Required: int"

I have done quite a bit of reading on accessing files but I couldn't find what I needed.

I would really appreciate your guidance on how to approach this problem.

Thank you for your time,

John

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code for reading a file looks a bit complicated. You just want to read a text file which contains a number (or several) in binary representation, right? If there's nothing else in a line, you can do that similar like this:



Hope this helps.
 
Marshal
Posts: 77891
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much simpler way to read a text file: find out about the Scanner class. You can dispense with the exception handling and all the Streams and Readers; the Scanner does all that for you.
You are however successfully reading the lines. Problem is, you are not doing anything with them. You need some way of passing the Strings you have read to another method.
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for such a quick response D. Ogranos.

Yes the text file will only be containing 1 binary number therfore somethin like this:111000101010101011011000010.
If I may ask with "BufferedReader input = new BufferedReader(new FileReader("myfile.txt"));" the name of my text file is "myfile.txt" which is pretty straight forward however where do I input the local path where "myfile.txt is located. I tried to use the same syntax " C:\\temp\\h12-2\\test.txt " but that didn't work.
How would I go about providing its path?

Thank you also Campbell Ritchie for the scanner suggestion, it sounds like a totally different approach; I will look it up and see if I understand its concepts.

I'm slow for I try out the code before asking

John
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was abtle to find that the location of the text file is by default the working directory of the netbean.

However I am running into a problem using what I am reading out of the text file. I am able to read the binary number and display it but I can't plug it in my formula to display its decimal equivalent.

Any suggestions would be greatly appreciated

 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really really new at java and I have jsut figured out why my output window was just giving me one thousand ones instead of the decimal to the E power as I was awaiting.

Apparently when using this method I can have one thousand ones converted to a decimal number:

public class test2
{
public static void main(String args[])
{
String bin = "1111111111111111111111111";
double ctr=1, decimal=0;

for (double i = bin.length()-1; i>=0; i--)
{
if(bin.charAt((int) i)=='1')
decimal+=ctr;
ctr*=2;
}
System.out.println ("Decimal: "+decimal);
}
}

but using this method:
int value = Integer.parseInt(line, 2);

I receive just the binary number not converted, just all the ones.

I really don't want to be annoying but could I ask what else I could do in order for me to convert that many 1 to a decimal equivalent. I looked at all hte option when I select the "." after Integer. like MAX_VALUE, getInterger, etc... but none seem like they would allow me to convert such a large binary number.

Once again thank you for your help.




 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I understand right, Integer.parseInt should convert your string with the binary number without problems. If the number you want to read is too large, then you may have to use Long.parseLong instead. And if that is not enough, perhaps have a look at java.Math.BigInteger: you can construct a BigInteger with the constructor BigInteger(String numstring, int radix), and display the decimal value with toString(int radix).
 
Campbell Ritchie
Marshal
Posts: 77891
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Philippe wrote:. . . I'm slow for I try out the code before asking . . .

Quite right. You will learn a lot better like that.

I would suggest you might find using a suitable text editor (eg jEdit, Notepad++, Notepad2, not Microsoft® Notepad) and the command-line easier than using NetBeans. We often find learning the IDE gets in the way of learning Java. Also NetBeans' directory structure can be confusing.
 
John Philippe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie for the tips for the editors.
 
Campbell Ritchie
Marshal
Posts: 77891
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic