-2

I have this code which reads an encrypted piece of a string (so, some garbage value when opened using notepad) but I keep getting "java.lang.NumberFormatException: Illegal embedded sign character".

FileReader fr = new FileReader("encrypted.txt"); 
Scanner sc = new Scanner(fr);
String s = sc.nextLine();

BigInteger OC = new BigInteger(s);

I wonder if it has something to do with the version of SDK I'm using to compile which keep giving me errors, or just mistake somewhere in the code.

Anoop Kanyan
  • 618
  • 7
  • 19
  • 3
    Well how would you *expect* it to read garbage text as an integer? – Jon Skeet Feb 15 '16 at 13:48
  • Reading a random bytes into a `java.lang.String` is a bad idea. You should read it as byte array and than convert bytes, if necessary. See this answer: http://stackoverflow.com/questions/858980/file-to-byte-in-java – Ihor Romanchenko Feb 15 '16 at 13:50
  • @JonSkeet kinda obvious, wonder why I missed that, thanks though! – user5844671 Feb 15 '16 at 14:42

1 Answers1

1

encrypted.txt file should have numbers and not encoded strings. If you change it, it will work. The reason is BigInteger() constructor expects a string which has only numbers.

Thanga
  • 7,811
  • 3
  • 19
  • 38
  • If it's encrypted then it may be in base 32, in which case using the [radix](https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger%28java.lang.String,%20int%29) version may be sufficient. – OldCurmudgeon Feb 15 '16 at 13:58