Results 1 to 20 of 20
  1. #1
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default how can i store 30 int digits without using bigInteger class

    I need to store int without using bigInteger class, bigDecimal class or anything like that. What else can i do?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    You could misuse some other data type like String, or bludgeon an int[] into doing the work of BigInteger.

  3. #3
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    i did try to use string. But i cannot convert it back into an int because its too big.

    im required to enter the number all at once and then input it into an array.

    can i possibly do it another way?
    Last edited by akeni; 11-09-2011 at 09:19 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    Before it was storing these 30 digits, now it's converting them "back" to an int. It might be easier if you actually said what you are doing. And, if you're doing arithmetic with these sets of digits, why you won't use BigInteger.

    Anyway, you can't convert 30 digits to an int. Not ever. And for the very reason you gave: the int type just isn't big enough. Likewise, and for the same reason, the 30 digits never were an int so there is no real question of converting them back back to that type.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: how can i store 30 int digits without using bigInteger class

    Quote Originally Posted by pbrockway2 View Post
    Anyway, you can't convert 30 digits to an int. Not ever.
    If those digits are binary digits from a binary representation ...

    kind regards,

    Jos (runs -----------------------------------------------------------> that a'way ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    Quote Originally Posted by pbrockway2 View Post
    Before it was storing these 30 digits, now it's converting them "back" to an int. It might be easier if you actually said what you are doing. And, if you're doing arithmetic with these sets of digits, why you won't use BigInteger.

    Anyway, you can't convert 30 digits to an int. Not ever. And for the very reason you gave: the int type just isn't big enough. Likewise, and for the same reason, the 30 digits never were an int so there is no real question of converting them back back to that type.
    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class lab8 {
        public static void main (String[] args){
            int [] int1= new int [30];
            int [] int2= new int [30];
            
            int [] x= input(int1);
            int [] y= input(int2);
    	
    	sum(x,y);
            
        }
    
        public static int [] input (int [] x){
            int i;
            System.out.println("input an integer less than 30 digits");
            Scanner myInput = new Scanner(System.in);
    	int b = myInput.nextInt();
    	    
            
            for (i=x.length-1; i>0;i--){
    	   
    	    x[i]= b%10;
                b /= 10;
    	    System.out.print(x[i]);
    	    if (b<0){
    		    i=0;
    	    }
            }
            return x;
        }
        
        public static void sum(int [] x, int [] y){
            int i;
            int [] sum= new int [30];
            for( i=sum.length;i>-1;i--){
                sum[i]= x[i]+y[i];
                if (sum[i]>=10){
                    sum[i] =sum[i]%10;
                    x[i-1]+=1;
                }
            }
        }
            
    }
    this is what i have so far.

    here is the description of the problem:

    p only one of the following programs.
    Write a java program to read two integer numbers each up to 30 digits, and then add them and print the result along with input numbers..
    Sample output:
    Example: Assume the first number is 39983928349458 and the second number is equal to 92387486729, then the output should be as follow:
    Output:


    39,983,928,349,458+
    92,387,486,729
    --------------------
    40,076,315,836,187
    Split your program into small methods.
    Note1: You should input your numbers without comma, but the output should be printed with comma as shown in the example.
    Note2: You are not allowed to use java's BigInteger, BigDecimal, or similar classes.

    edit*

    So after i have received an input of integer, i have to store the number in a size 30 array. Hence why i have arrays in the program.

    thanks for all the help so far.
    Last edited by akeni; 11-09-2011 at 11:25 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    Let's think about types: the types of variables, the types you send to methods, the types you get back.

    "Write a java program to read two integer numbers each up to 30 digits". We know for sure the type we won't be reading from the keyboard. It won't be an int because ints can't represent 30 digit integer numbers. I would suggest you read String types from the keyboard.

    Java Code:
    //int b = myInput.nextInt();
    String line = myInput.nextLine();
    (Notice the more descriptive variable. b is less meaningful than line.)

    Now the input() method's job is to create an int[] representing the input. (So you have already decided how you are going to represent 30 digit integer values: as int[]. That's a good choice in my opinion because you are later going to do some arithmetic on them and that's not easy with String values.)

    Your for loop needs to be changed if you accept a String from the keyboard. You will have to break the string up into characters (look in the String class for something that does this). Then for each character you need the integer value that corresponds to the character. Eg '8' corresponds to 8. This has nothing to do with the char value of '8', rater there is a digit() method in the Character class that will return the correct value as an int.

    One final point about this method: you pass it an int[] and you return an int[] (the same one you passed). There is no need to do both. Probably it is best not to use the int[] argument.

    -----

    At this point you should remove the sum() method and make sure you can successfully read a 30 digit integer value (as a String) and create an int[] containing the appropriate 30 ints.

  8. #8
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    right now im trying to get a input as string, and then convert it to decimal value. But after that, how can i turn the ASCII decimal value into the the int character?

    for EG. if my input is 123, the ASCII would be 49,50,51 respectively. Since i converted to ASCII decimal value, how do i turn it back into 123 of integer value?

    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class lab8_2 {
        public static void main (String[] args){
    	    
    	    char [] number1;
    	    char [] number2;
    	    
    	    number1=getmsg();
    	    number2=getmsg();
    	    
    	    int [] x= convert(number1);
    	    int [] y= convert(number2);
    	    
    	    
    	    
        }
        
        public static char [] getmsg(){
    	String myMessage;
    	System.out.print("Input : ");
    	Scanner myIn = new Scanner(System.in);
    	myMessage = myIn.nextLine();// Read a line of message
    	
    	while (myMessage.length()>30){
    		System.out.println("Digits too long, re-enter");
    		myMessage= myIn.nextLine();
    	}
         
    	
    	   
         return myMessage.toCharArray();
        }
        
        public static int []  convert (char [] a){
    	    
    	    int i;
    	    int [] b = new int [a.length];
    	    
    	    for (i=0;i<a.length;i++){
    		    b[i]= (int) a[i];
    	    }
    	    
    	    
    	    for (i=0;i<b.length;i++){
    		    System.out.print(b[i]);
    	    }
    	return b;
        }
        
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how can i store 30 int digits without using bigInteger class

    Why do you need to use ASCII? Have you not been listening? You cannot convert it to an int because the number will be too large. Leave it as a String or char array or int array or whatever.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    for EG. if my input is 123, the ASCII would be 49,50,51 respectively. Since i converted to ASCII decimal value, how do i turn it back into 123 of integer value?
    When you read a '1' char value you do not the ASCII int value 49. (Because this is a meaningless value when it comes to doing arithmetic). Rather you want the int value that really corresponds to '1': that is the int value 1. Look at the digit() method I have already linked to - the "radix" it talks about is the base: 10 in this case.

    Java Code:
    for (i=0;i<a.length;i++){
            // (int)a[i] will give the char or ASCII value of the character which is no good
        //b[i]= (int) a[i];
        b[i] = ... //use the digit() method
    }
    (Keep in mind for later that the returned int[] will be of different lengths for different inputs.)

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    Or say

    Java Code:
    b[i] = (int)a[i] - '0';
    It gets to the same place. I only mention this because someone else is sure to. It's not as obvious, or as general as using the library method, digit(), that was put there precisely for this sort of conversion.

  12. #12
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    Sorry if i wasn't interpreting myself well enough. I know i cannot fit more than 10 digits into an Int. However i can't use bigInteger class to solve my problem. pbrockway2's workaround works beautifully. Now i have each digit in a different section of the array, i can sum up the digits per array box, and complete the specific addition task.

    Another question, how can i set the string length to be 30, even if i enter less than 30 characters?

    I want to thank everyone for their insight and help so far.
    Last edited by akeni; 11-10-2011 at 02:43 AM.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    Another question, how can i set the string length to be 30, even if i enter less than 30 characters?
    This is what I was getting at with my parenthetic remark at the end of #10.

    Basically you can't do anything about the string length. You get what you get. Likewise when you call toCharArray(): you get a char[] and Java arrays never change their length.[*]

    The one array you can control - because you create it yourself - is the int[] array you create in convert(). You could make it 30 elements long. It will get zeros in any slots you don't actually assign.

    (If you make a 30 element int[] array, which end will you fill it from?)

    EDIT:[*] These statements are true, but they're not the whole truth. You can create another string padded on the left with zeros, or copy the char[] into another longer one. But I think creating a 30 element int[] on which you do the arithmetic is more straight foward.
    Last edited by pbrockway2; 11-10-2011 at 03:23 AM.

  14. #14
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    okay, a small update. this is what my program looks like so far:

    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class lab8_2 {
        public static void main (String[] args){
    	    
    	    char [] number1;
    	    char [] number2;
    	    
    	    number1=getmsg();
    	    number2=getmsg();
    	    
    	    int [] x= convert(number1);
    	    int [] y= convert(number2);
    	    
    	    
    	    sum(x,y);
    	    
    	    
    	    
        }
        
        public static char [] getmsg(){
    	String myMessage;
    	System.out.print("Input : ");
    	Scanner myIn = new Scanner(System.in);
    	myMessage = myIn.nextLine();// Read a line of message
    	
    	while (myMessage.length()>30){
    		System.out.println("Digits too long, re-enter");
    		myMessage= myIn.nextLine();
    	}
         
    	
    	   
         return myMessage.toCharArray();
        }
        
        public static int []  convert (char [] a){
    	    
    	    int i;
    	    int j=a.length-1;
    	    int [] b = new int [31];
    	    
    	    
    	    
    	    for (i=b.length-1;i>0;i--){
    		    if (j>-1){
    			    b[i]= ((int) a[j]-'0');
    			    j--;
    		    }
    	    }
    	    
    	return b;
        }
        
        public static void  sum (int [] a, int [] b){
    	    
    	    int i;
    	    int [] sum= new int [a.length];
    	    int j;
    	    
    	    for (i=a.length-1;i>0;i--){
    		    sum[i]= a[i]+b[i]+sum[i];
    		    
    		    if (sum[i]>=10){
    			    sum[i]= sum[i]%10;
    			    if (i>1){
    			    sum[i-1]++;
    			    }
    		    }
    		     System.out.print(+sum[i]);
    	    }
    	    
    	  
    	    
    	    
        }
    }
    when it prints, it shows all the f***** zeros. any tricks to delete them?

    edit* the sum array is reversed atm. I am making a method to reverse it back to reading it from left to right.

    thanks for the warning about swearing. pretty fustrated atm lol.
    Last edited by akeni; 11-10-2011 at 05:43 AM.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how can i store 30 int digits without using bigInteger class

    Please don't fargin swear. I'm sure it is against forum rules.

    You will need to know where the zeros end and the real number starts and only print the real number. If you think about this logically there should be at most one zero at the front of your array. If you add 999 and 999 the length of the result will be 4. It is impossible for your result to be more than one digit longer than either of the operands. So do a check if the first value is 0 trim it before printing. This would work if you store the result in a StringBuilder.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    when it prints, it shows all the fucking zeros. any tricks to delete them?

    edit* the sum array is reversed atm. I am making a method to reverse it back to reading it from left to right.
    No tricks. But in terms of types, your 30 digit integers got read in as String, then split into char[], then converted into int[]. Now that you've performed the arithmetic and you want to display the answer you have to ... go back to String! Basically when you build up this String you ignore any leading zeros. (There will be one special case here: when the sum really is zero.)

    Note: the assignment also asks for some special formatting of this String. Do this as you build the string up from the values of the int[].

    -----

    The only positive thing about hitting your head against this brick wall of conversions is how good it feels when you stop, and are allowed to use BigInteger...

  17. #17
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    okay, so i figured it out with you guys :P. Coding could be better but this is what i have so far:
    Java Code:
    import java.util.*;
    import java.lang.*;
    
    public class lab8_2 {
        public static void main (String[] args){
    	    
    	    char [] number1;
    	    char [] number2;
    	    
    	    number1=getmsg();
    	    number2=getmsg();
    	    
    	    int [] x= convert(number1);
    	    System.out.println("");
    	    System.out.print("+");
    	    int [] y= convert(number2);
    	    System.out.println("");
    	    
    	    System.out.println("---------------------------------");
    	    sum(x,y);
    	    
    	    
    	    
        }
        
        public static char [] getmsg(){
    	String myMessage;
    	System.out.print("Input : ");
    	Scanner myIn = new Scanner(System.in);
    	myMessage = myIn.nextLine();// Read a line of message
    	
    	while (myMessage.length()>30){
    		System.out.println("Digits too long, re-enter");
    		myMessage= myIn.nextLine();
    	}
         
    	
    	   
         return myMessage.toCharArray();
        }
        
        public static int []  convert (char [] a){
    	    
    	    int i;
    	    int j=a.length-1;
    	    int [] b = new int [32];
    	    
    	    
    	    
    	    for (i=b.length-1;i>0;i--){
    		    if (j>-1){
    			    b[i]= ((int) a[j]-'0');
    			    j--;
    			    
    		    }
    	    }
    	    i=0;
    	    
    	    while (b[i]==0){
    			    i++;
    		    }
    	    while (i<b.length){
    		   
    		    System.out.print(b[i]);
    		    i++;
    	    }
    	    
    	    
    	return b;
        }
        
        public static void  sum (int [] a, int [] b){
    	    
    	    int i;
    	    int j;
    	    
    	    int [] sum = new int [a.length];
    	
    	    
    	    for (i=sum.length-1;i>0;i--){
    		    sum[i]= a[i]+b[i]+sum[i];
    		    
    		    if (sum[i]>=10){
    			    sum[i]= sum[i]%10;
    			    sum[i-1]++;
    		    }
    	    }
    	    
    	    int [] reverse= reverse(sum);
    	    
    	    
    	    i=0;
    	    while (reverse[i]==0){
    			    i++;
    		    }
    		    
    		    
    		    
    	    while (i<reverse.length){
    		
    		    System.out.print(reverse[i]);
    		    i++;
    	    }
    	    
        }
        
        public static int [] reverse (int [] sum){
    	
    	int [] tmp = new int [sum.length];
    	int i;
    	int j=sum.length-1;
        
    	for (i=sum.length-1;i>0;i--){
    		if (j>0){
    			tmp[i]=sum[j];
    			j--;
    		}
    		
    	}
    	
    	return tmp;
       }
    }
    it works and it takes in one more digit just in case the 30 digits plus the other 30 digits add up to more than 30 digits.

    edit* this isn't in string form. i basically put each notation into a specific array box. And then i added the the digits up, and i move to the next box.

    it'll take me some time to figure out how to turn it back to string, so i'll just leave it like this for now since it is functional.

    again, thanks for all the help ^^ i really appreciate it.
    Last edited by akeni; 11-10-2011 at 06:47 AM.

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i store 30 int digits without using bigInteger class

    it'll take me some time to figure out how to turn it back to string, so i'll just leave it like this for now since it is functional.

    again, thanks for all the help ^^ i really appreciate it.
    You're welcome.

    It's OK to print these things character by character - it's up to you really. (Although a lot of people stress the idea of doing one thing per method. ie either convert or print, but not both.)

    I notice from a previous post that you had the + after the first number not in front of the second. And that you had right aligned them. Also I'm wondering what does happen if the user enters two zeros. Just being picky, so feel free to ignore this...

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how can i store 30 int digits without using bigInteger class

    Quote Originally Posted by pbrockway2 View Post
    Also I'm wondering what does happen if the user enters two zeros.
    I didn't look at the OP's code but my solution handles it correctly. "0" + "0" = "00" then you drop the first char if it is '0' => "0".

  20. #20
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how can i store 30 int digits without using bigInteger class

    Yeah, i'm trying to get fix these problems right now lol. 0+0 crashes the program :D

Similar Threads

  1. Replies: 3
    Last Post: 10-27-2011, 06:23 PM
  2. Replies: 4
    Last Post: 03-07-2011, 02:24 PM
  3. My own BigInteger class. Need help.
    By Dinde in forum New To Java
    Replies: 2
    Last Post: 01-27-2010, 09:49 PM
  4. help me to store data in a class
    By neeraj.singh in forum New To Java
    Replies: 2
    Last Post: 08-06-2009, 09:31 AM
  5. Class to store Image+Text
    By Gudradain in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 09:32 AM

Posting Permissions

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