1

This is my code:

public class sample {
   public static void main(String []args) throws Exception {

      System.out.println("Enter from file:");

      BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\KK\\A Key.txt"));

      String currentline;

      while((currentline=br.readLine())!=null){

         System.out.println(currentline);
      }

      BigInteger a = new BigInteger(currentline); 

      System.out.println(a);

  }

I want to read from the text document , convert it into big integer from a string, I tried this but i get a run time error , How to convert String into corresponding Big integer Ascii value.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
kumar TN
  • 21
  • 1
  • 7

1 Answers1

10

Your problem is very simple: you build a BigInteger from a null string!

You are looping until currentLine is null.

Afterwards you try to create a BigInteger from that!

Simply move things into your loop:

while((currentLine=br.readLine())!=null) {           
 System.out.println(currentLine);
 BigInteger a = new BigInteger(currentline); 
 System.out.println(a);
}

Et voilà, things are working (assuming that you expect each line in your file to be a number). If the whole file represents one number, then you must do as zstring suggests in his comment: you would have to use a StringBuilder for example to "collect" all lines into a single string that you then use to create the BigInteger object.

And please note java coding styles: class names start Uppercase; and variable names use camelCase (thus I changed to currentLine).

But just for the record: you wrote a low quality question. You should always include compiler error messages or stack traces when asking "why is my code not working".

GhostCat
  • 137,827
  • 25
  • 176
  • 248