Results 1 to 4 of 4
  1. #1
    wiggity is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Java program to calculate numbers longer than long datatype can hold.

    hello, I am trying to write a java program that will add and multiply numbers that can have up to 100 digits. here is what i have so far.
    Java Code:
    import java.util.*;
    
    public class Mult {
     	private static Scanner in = new Scanner (System.in);
    
     public static void main (String[] args) {
    	int test = 0;
    	do{
      		System.out.print ("Enter the first number:  ");
      		String a = in.nextLine();
    		String y = a;
      		boolean check = isValid (y);
      		if(check == true){
    			test = 2;
    		}
    	}while(test < 1);
    	System.out.println(a);
    	int test2 = 0;
    	do{
    		System.out.print("Enter the second number: ");
    		String b = in.nextLine();
    		String x = b;
    		boolean check2 = isValid(x);
    		if(check2 == true){
    			test2 = 2;
    		}
     	}while(test2 < 1);
    	System.out.println(b);
    	String T = add(a, b);
    	System.out.println(T);
    }
    	public static boolean isValid (String num) {
    		for (int i = 0; i < num.length(); i++) {
    	    		char c = num.charAt(i);
    	    		if ((c >= '0') && (c <= '9')){
    	    		return true;
    			}
    	  	}
    		return false;
    	}
    	public static String noLeadingZeroes (String num){
    		int count = 0;
    		for (int i = 0; i < num.length(); i++) {
    	    		char c = num.charAt(i);
    	    			if(c == 0){
    					count = count + 1;
    				}
    				else{
    					i = num.length();
    				}
    		}
    		return num;
    	}
    	public static String add (String a, String b){
    		while (a.length() < b.length()) a = "0"+a;
    		while (b.length() < a.length()) b = "0"+b;
    		int c = 0;
    		String total = "";
    		for (int i=a.length()-1; i>=0; i--) {
    			int adig = a.charAt(i)-'0';
      			int bdig = b.charAt(i)-'0';
       			int ddig = 0;
    			if (adig+c+bdig > 9) {
        				ddig = adig+c+bdig-10;
        				c = 1;
       			} 
    			else {
       		 		ddig = adig+c+bdig;
        				c = 0;
    			}
    		total = ((char)(ddig + '0'))+total;
    		}
    	return total;
    
    	}	
    }
    when i comment out the check isValid method the program runs. and I recieve the correct answer. where are my a and b variables getting lost or corrupted?

    thanks for any help...

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java program to calculate numbers longer than long datatype can hold.

    Try the BigInteger class.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    wiggity is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java program to calculate numbers longer than long datatype can hold.

    Quote Originally Posted by Norm View Post
    Try the BigInteger class.
    This is for an assignment. The point of the excersice is to use strings. I just need to know where in my code my a and b variables are changing. I know it is somewhere in the isValid method.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Java program to calculate numbers longer than long datatype can hold.

    where in my code my a and b variables are changing
    Try debugging the code by adding printlns that print out the values of all the variable as they are given values and as they are used. The print out will should you where those variables are changing.
    Be sure to add an ID String with the println("var=" + var) where var is the name of the variable you are printing so you know what the variable is that is printed and where it was printed.

    Your code does not compile without errors. You need to fix the errors in the code BEFORE you try to execute it.
    Last edited by Norm; 04-18-2012 at 02:13 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 06-30-2011, 09:01 PM
  2. Extracting individual numbers from a long integer
    By CurbYourEnthusiasm in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 03:53 PM
  3. Program in Java To calculate GCD of n numbers.?
    By ankitsinghal_89 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:23 AM
  4. Replies: 9
    Last Post: 05-21-2010, 10:46 AM
  5. Calculate sum of long integer!
    By Julingo in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 11:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •