Sorry for my poor english as i'm not a native english speaker and writer, but i have written a code which calculates big numbers, adding subtracting and multiplying.
when multiplying, adding and subracting numbers, it works, but when i try adding 0 to 0 or subtracting 0 to 0 or multiplying 0 to 0 it does not seem to work. also for when multiplying a number with 0 or vice versa, it also does not work :(( i have no idea why can somehelp me please?

Can you post some code? Have you tried debugging your code by adding some System.out.println messages to see what is happenig as the code runs?

sorry i did not post the code, i tried again, but for every result that becomes 0 does not seem, to work... is there a problem with the input? under the public static void method?

``````import java.io.*;

public class bigInteger3 {

void caculator(byte[] N,char O,byte[] M){

switch(O){
case '-' :
negate(M);

case '+' :

plus(N,M);

break;

case '*' :

multiply(N,M);

break;

default :

System.out.println("잘못된 입력");
break;
}
}

void optim(byte[] X,byte top,int len){
if(top>0){
for(int i=0;i<len;i++){
if(X[i]<0){
X[i]+=10;
X[i+1]--;
}
if(X[i]>9){
X[i]-=10;
X[i+1]++;
}

}

}
else if(top<0){
for(int i=0;i<X.length-1;i++){
if(X[i]>0){
X[i]-=10;
X[i+1]++;
}
if(X[i]<-9){
X[i]+=10;
X[i+1]--;
}

}
}
else
optim(X,X[len-2],len-1);
}

void optim(byte[] X,int x,int y){

X[x+y+1]+=(byte)(X[x+y]/10);
X[x+y]=(byte)(X[x+y]%10);

}

void reverse(byte[] X,int len){
if(len==1)
return;
byte temp=X[0];
for(int i=0; i<len-1; i++){
X[i]=X[i+1];
}
X[len-1]=temp;

reverse(X,len-1);
}

void negate(byte[] M){

for(int i=0;i<M.length;i++)
M[i]=(byte)-M[i];

}

static String trim(byte[] B){
int c=0,index=0;
String s="";

for(int i=0;i<B.length;i++){
s=s+Byte.toString(B[i]);
}

while(s.charAt(index)=='0'){index++;c++;}
s=s.substring(c);
if(s.charAt(0)=='-'){
s=s.replaceAll("-","");
s="-"+s;
}
return s;
}

byte[] LargerOfTwo(byte[][] T){
if(T[0].length>T[1].length)
return T[0];
else return T[1];
}

int Min(int x,int y){
if(x>y) return y;
else return x;
}
int Max(int x,int y){
if(x>y) return x;
else return y;
}

/*   void plus2(byte[] N,byte[] M){

int L1=N.length;
int L2=M.length;
int min=Min(L1,L2);
int max=Max(L1,L2);
int L3=max+1;
reverse(N,L1);
reverse(M,L2);

byte[][] Temp={N,M};
byte[] Tempp=LongOfTwo(Temp);
byte[] Sum=new byte[max+1];

for(int i=0;i<min;i++)
Sum[i]=(byte)(N[i]+M[i]); //아래숫자더하기
for(int i=min;i<max;i++)
Sum[i]=(byte)(Tempp[i]);  //위에 숫자 더하기

optim(Sum,Sum[L3-1],L3);
reverse(Sum,L3);
System.out.println(trim(Sum));

};
*/
void plus(byte[] N,byte[] M){

int L1=N.length;
int L2=M.length;
int L3=Max(L1,L2)+1;
int L4=Min(L1,L2);
byte[] Sum=new byte[L3];

reverse(N,L1);
reverse(M,L2);

byte[][] Temp={N,M};
byte[] Tempp=LargerOfTwo(Temp);

for(int i=0;i<Tempp.length;i++)
Sum[i]=(byte)(Tempp[i]);

for(int j=0;j<L4;j++)
Sum[j]=(byte)(N[j]+M[j]);

optim(Sum,Sum[L3-1],L3);
reverse(Sum,L3);
System.out.println(trim(Sum));
/*
String s="";
for(int i=0;i<Sum.length;i++){
s=s+Byte.toString(Sum[i])+"|";
}

System.out.println(s);
*/

};

void minus(byte[] N,byte[] M){
negate(M);
plus(N,M);
}; //negation호출,plus호출};

void multiply(byte[] N,byte[] M){

int i=0,j=0;
int L1=N.length,L2=M.length,MAX=L1+L2+1;
byte[] Summation=new byte[MAX];
reverse(N,L1);
reverse(M,L2);
for(i=0;i<L2;i++){
for(j=0;j<L1;j++){
Summation[i+j]=(byte)(Summation[i+j]+M[i]*N[j]);

optim(Summation,i,j);
}

}

reverse(Summation,MAX);
System.out.println(trim(Summation));
}

byte[] calculation(String OrginInput){

String input=OrginInput.replaceAll(" ","");

int index=0,count=0,start=0,maxlen=input.length();
byte[] Number;
//System.out.println("maxlen = "+maxlen);
while(!Character.isDigit(input.charAt(index))){
//System.out.println("index = "+index);
if(input.charAt(index)=='-')count++; index++;
}
start=index;
while(index<maxlen&&Character.isDigit(input.charAt(index))){
//System.out.println("index = "+index);
index++;

}

Number=new byte[index-start];

for(int j=0;j<Number.length;j++){
Number[j]=(byte)(input.charAt(start+j)-48);
}

if(count%2==1)negate(Number);

/////여기까지 숫자

if(index<maxlen)
caculator(Number,input.charAt(index),calculation(input.substring(index+1)));

return Number;
}

public static void main(String[] args){
//숫자입력
//숫자를 자르기
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while(true){
bigInteger3 bi= new bigInteger3();
try{
String OrginInput = br.readLine();
if (OrginInput.compareTo("quit") == 0)
{

break;
}

bi.calculation(OrginInput);

}
catch (Exception e)
{

System.out.println("입력이 잘못되었습니다. 오류 : " + e.toString());
}

}

}

}
``````

should i try putting in some System.out.println(""); codes in the methods to see where it is not working from?

If I am to call it, what argument should I pass?

should i try putting in some System.out.println(""); codes in the methods to see where it is not working from?

Yes, absolutely yes. That's always a good thing to do right from the start.

I have been looking at your code and i don't understand the byte[] calculation(String OrginInput) method. If it is used to "extract" the numbers from the input there are better ways to do it. Also why do you call the negate method inside:

``````if (count % 2 == 1)
negate(Number);
``````

When you can simply call the caculator with a minus?
Also calling the calculation method again with recursion isn't very smart.
From what I understand you are trying to convert numbers into byte arrays. I tried simplifying your code. Is this example correct:

``````String input2 = "56";
byte[] Number2 = new byte[input2.length()];
for (int j = 0; j < Number2.length; j++) {
Number2[j] = (byte) (input2.charAt(j) - 48);
}
``````

If so then after testing I found that there is a problem when the result is zero. Not when you are trying to add or multiply with zero.

``````while (s.charAt(index) == '0') {
index++;
c++;
}
``````

At the above code if s is all zeros (s="000") then the expression at the while will always be true. The index will keep increasing and you will get an exception. When you call the charAt you always need to check the length of the String compering it with the index you use. It is like looping an array:
for (int i=0;i<arr.length;i++)

``````while (index<s.length() && s.charAt(index) == '0') {
index++;
c++;
}
s = s.substring(c);
if (s.length()>0 && s.charAt(0) == '-') {
s = s.replaceAll("-", "");
s = "-" + s;
}
``````

Try calling in the main method method something like this and test it for various inputs:

``````BigInteger3 bi = new BigInteger3();
String input1 = "......";
// convert input1 to an array of bytes

String input2 = "......";
// convert input2 to an array of bytes

bi.caculator(Number1, '+', Number2);
bi.caculator(Number1, '-', Number2);
bi.caculator(Number1, '*', Number2);
``````

Then try to work your way with parsing the input. Can you post an example of inputs that you would normally pass?

Yes, your simplification code is correct.
ohhhh, so in the while loop, it will be true when s is 0 and keep looping causing the exception error?
i'll try the main method you have suggested :)
example of inputs i would try are big numbers such as 1000000000000000+1000000000000000 <- these.
but for those large numbers it works, just for when the result is 0.
i tried adding the System.out.println("worked succesfully"); at the end of all methods, but when running the code, it does not seem to be printing out that line for all methods, for example, i have 9 of those lines, but only 7 show when it seems to work(for this i'm guesssing that some of the methods are not used so not all shows), but only 2 shows for when the result is 0(for this i have tried looking back through the methods but it seems i'm not that much of a debugger person :(((((((

i'm not that much of a debugger person

All it needs is a little practice, so don't be worried! Instead of just printing "OK", print some of the actual data values, so you can see exactly what's happening. For example, in your trim method you could add some prints like this...

``````static String trim(byte[] B){
int c=0,index=0;
String s="";
for(int i=0;i<B.length;i++){
s=s+Byte.toString(B[i]);
}

System.out.println("1. s= " + s); // was s constructed properly?

while(s.charAt(index)=='0'){index++;c++;}
s=s.substring(c);

System.out.println("2. s= " + s); // now what's happened to s?

if(s.charAt(0)=='-'){
s=s.replaceAll("-","");
s="-"+s;
}

System.out.println("3. s= " + s); // and what value are we returning?

return s;
}
``````

(Note the use of text like "2. s=" so you can see what output is which when there's a lot of printout)

ps to print an array in a readable format you can use the following example:

``````System.out.println("N= " + Arrays.toString(N)); //  where N is an array
``````

thank you ~.~