0

Maybe this question is silly, but I'm new in Java and I cant figure this out...

So I have two clases - Digit and BigDigit and the problem is here Digit2.sub(Digit1); when Digit1 is negative I get

Exception in thread "main" java.lang.NumberFormatException: Illegal embedded sign character at java.math.BigInteger.(Unknown Source)

but when I tried pass as argument not Digit1, but example "-18370", then everything is ok, but basically it should be the same. If it's positive, everyting works like a charm. Can anyone give me a tip where I went wrong? Method 'add' works all the time, but 'sub' only with positive Digit1.

UPDATED This code works when Digit1 is positive or if uncomented Digit1.add( Digit2);, then works when sum is positive. But not negative :(

import java.math.BigInteger;

public class Digit {

public static void main(String[] args)
{
    BigDigit Digit1 = new BigDigit("-18730");
    BigDigit Digit2 = new BigDigit("77730");

   // Digit1.add( Digit2);
    Digit1.display();
    Digit1.reverse();
    Digit1.display();

   Digit2.sub( Digit1);
   Digit2.display();
   Digit2.reverse();
   Digit2.display();
    }
}
class BigDigit {
public String number;
public BigInteger first;
public BigInteger second;

public BigDigit(String str) {number = str;}

public String add(BigDigit sk) {
    first = new BigInteger(number);
    second = new BigInteger(sk.number);
    return number = first.add(second).toString();
}

public String reverse() {
    return number = new StringBuffer(number).reverse().toString();
}

public void sub(BigDigit sk) {
    first = new BigInteger(number);
    second = new BigInteger(sk.number);     
    }
public void display() {System.out.println(number);}
}
3
  • 1
    I thought that error meant you had a - in the middle of the number, not at the start. Can you provide an example we can run? BTW You don't appear to need a first or second field, nor do you need to store it as a String. Commented Oct 12, 2013 at 21:56
  • Your code works for me (i added the missing 2 braces assuming they were supposed to be 2 seperate top level classes) Commented Oct 12, 2013 at 23:05
  • How could it be possible to get - in the middle of the number? Commented Oct 13, 2013 at 10:28

1 Answer 1

2

The problem is that you are trying to construct a BigInteger with an invalid number (03781-). This string is created by reversing -18730 in your reverse method.

This is what is printed without the addition:

C:\>java Digit
-18730
03781-
Exception in thread "main" java.lang.NumberFormatException: Illegal embedded sign character
        at java.math.BigInteger.<init>(Unknown Source)
        at java.math.BigInteger.<init>(Unknown Source)
        at BigDigit.sub(Digit.java:42)
        at Digit.main(Digit.java:15)

This is what's printed when I enable the addition

C:\>java Digit
59000
00095
77730
03777

Reversing a positive integer (59000) won't leave the embedded minus sign (00095), which is the source of the exception you are seeing.

Sign up to request clarification or add additional context in comments.

1 Comment

You just saved my day! Such a silly mistake and I tried to find problem elsevear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.