0

I'm doing an assignment where the goal is to, among other things, to add two large integers. Here is my code, spread out into four files.

Main that we cannot change:

import java.util.*;
import MyUtils.MyUtil;

public class CSCD210HW7
{
   public static void main(String [] args)throws Exception
   {
      int choice;
      String num;
      LargeInt one, two, three = null;
      Scanner kb = new Scanner(System.in);

      num = HW7Methods.readNum(kb);
      one = new LargeInt(num);

      num = HW7Methods.readNum(kb);
      two = new LargeInt(num);

      do
      {
         choice = MyUtil.menu(kb);

         switch(choice)
         {
            case 1:  System.out.println(one + "\n");
                     break;

            case 2:  System.out.println("The value of the LargeInt is: " + two.getValue() + "\n");
                     break;

            case 3:  num = HW7Methods.readNum(kb);
                     one.setValue(num);
                     break;

            case 4:  if(one.equals(two))
                        System.out.println("The LargeInts are equal");
                     else
                        System.out.println("The LargeInts are NOT equal");        
                     break;

            case 5:  three = two.add(one);
                     System.out.printf("The results of %s added to %s is %s\n", one.getValue(), two.getValue(), three.getValue());
                     break;  

            case 6:  HW7Methods.displayAscendingOrder(one, two, three);
                     break;

            default:  if(two.compareTo(one) < 0)
                        System.out.printf("LargeInt %s is less than LargeInt %s\n", two.getValue(), one.getValue());   
                     else if(two.compareTo(one) > 0)
                        System.out.printf("LargeInt %s is greater than LargeInt %s\n", two.getValue(), one.getValue());
                     else
                        System.out.printf("LargeInt %s is equal to LargeInt %s\n", two.getValue(), one.getValue());      
                     break;
         }// end switch

      }while(choice != 8);

   }// end main

}// end class

LargeInt Class(Custom Class We Created)

public class LargeInt implements Comparable<LargeInt>
{
   private int[]myArray;

   private LargeInt()
   {
      this("0");
   }

   public LargeInt(final String str)
   {
      this.myArray = new int[str.length()];
      for(int x = 0; x < this.myArray.length; x++)
      {
         this.myArray[x] = Integer.parseInt(str.charAt(x)+ "");
      }
   }

   public LargeInt add(final LargeInt passedIn)
   {
      String stringOne = myArray.toString();
      String stringTwo = passedIn.myArray.toString();

      int r = Integer.parseInt(stringOne);
      int e = Integer.parseInt(stringTwo);
      int s = r + e;
      return new LargeInt(""+s);
   }

   public void setValue(final String arrayString)
   {
      this.myArray = new int[arrayString.length()];
      for(int x = 0; x < myArray.length; x++)
      {
        this.myArray[x]=arrayString.charAt(x);
      }
   }

   @Override
   public int compareTo(LargeInt passedIn)
   {
      if(passedIn == null)
      {   
         throw new RuntimeException("NullExceptionError");
      }   
      int ewu = 0;
      int avs = 0;

      if(this.myArray.length != passedIn.myArray.length)
      {   
         return this.myArray.length - passedIn.myArray.length;
      }
      for(int i = 0; i < this.myArray.length -1; i++)
      {
         if(this.myArray[i] != passedIn.myArray[i])
         {
            return this.myArray[i]-passedIn.myArray[i];
         }
      }
      return ewu-avs;
   }
   public int hashCode()
   {
      String p = "";
      for(int f = 0; f < this.myArray.length; f++)
      {
         p += myArray[f]; 
      }

      return p.hashCode();
   }

   public String getValue()
   {
      String h = "";
      for(int t = 0; t < this.myArray.length; t++)
      {
         h += myArray[t]; 
      }
   return h;
   }   

   @Override
   public boolean equals(Object jbo)
   {
      if(jbo == null) 
      {   
         return false;
      }
      if(!(jbo instanceof LargeInt))
      {   
         return false;
      }
      LargeInt k =(LargeInt)jbo;
      if(k.myArray.length != this.myArray.length)
      {   
         return false;
      }   
      for(int d = 0; d < this.myArray.length; d++)
      {
         if(k.myArray[d] != myArray[d])
         {   
            return false;          
         }     
      }

      return true;
   }

   @Override
   public String toString()
   {
      String c = "";
      for(int q = 0; q < this.myArray.length; q++)
      {
         c += myArray[q]; 
      }
   return "The LargeInt is: " + c;
   }
 }

HW7Methods File

import java.util.*;
import java.io.*;

public class HW7Methods
{
   public static String readNum(Scanner kb)
   {
      String num = "";

      System.out.print("Enter Your Large Int: ");

      num = kb.nextLine();

      return num;
   }
   public static void displayAscendingOrder(final LargeInt first, final LargeInt second, final LargeInt third)
   {
      String highestInt;
      if(first.compareTo(second) >= 0 && first.compareTo(third) >= 0)
      {   
         highestInt = first.getValue();
      }
      else if(second.compareTo(first) >= 0 && second.compareTo(third) >= 0)
      {   
         highestInt = second.getValue();
      }
      else
      {   
         highestInt = third.getValue();
      }   
      String middleInt;
      if(first.compareTo(second) >= 0 && first.compareTo(third) <= 0)
      {   
         middleInt = first.getValue();
      }
      else if(second.compareTo(first) >= 0 && second.compareTo(third) <= 0)
      {
          middleInt = second.getValue();
      }
      else
      {
         middleInt = third.getValue();
      }   
      String lowestInt;
      if(first.compareTo(second) <= 0 && first.compareTo(third) <= 0)
      {
         lowestInt = first.getValue();
      }
      else if(second.compareTo(first) <= 0 && second.compareTo(third) <= 0)
      {
         lowestInt = second.getValue();
      }
      else
      {
         lowestInt = third.getValue();
      }   
      System.out.println("The LargeInts in order are: " + lowestInt + ", " + middleInt + ", " + highestInt);

   }
}

MyUtil file

package MyUtils;
import java.io.*;
import java.util.Scanner;

public class MyUtil
{  
   public static int menu(Scanner kb)
   {
      int userChoice;
      System.out.println("1) Print First Int");
      System.out.println("2) Print Second Int");
      System.out.println("3) Add Different Int");
      System.out.println("4) Check If Equal");
      System.out.println("5) Add Large Ints");
      System.out.println("6) Display In Ascending Order");
      System.out.println("7) Compare Ints");
      System.out.println("8) Quit");



      kb = new Scanner(System.in);

      System.out.print("Please Select Your Choice: ");
      userChoice = kb.nextInt();

      while(userChoice < 1 || userChoice > 8)
      {
         System.out.print("Invalid Menu Choice. Please Re-Enter: ");
         userChoice = kb.nextInt();
      }

      return userChoice;
   }
}

When I go to run this code, it prompts me for two Large Integers like it's supposed to. However, when I choose option 5 to add them, this is what I get:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[I@55f96302"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at LargeInt.add(LargeInt.java:24)
    at CSCD210HW7.main(CSCD210HW7.java:41)

I've never seen that type of error before. Can someone tell me what is going on?

2
  • I think the idea with the add method is that you're supposed to add the two LargeInt numbers digit by digit. What you're trying to do is convert them both back into int and then add them - but that's kind of not the point of a "large integer" class. This is for storing numbers that are too big to fit in the int data type. So my guess is that you're approaching the assignment all wrong. Commented Nov 22, 2014 at 1:52
  • Interesting how similar this is: stackoverflow.com/questions/27072499/… (I mean the code) Commented Nov 22, 2014 at 1:53

1 Answer 1

3

For input string: "[I@55f96302

That is not a "proper" String you are trying to parse here.

This is what an int[] looks like when you call toString() on it.

String stringOne = myArray.toString();

Why do you do that? What is that supposed to do?

int r = Integer.parseInt(stringOne);
int e = Integer.parseInt(stringTwo);
int s = r + e;

From the looks of it, you try to handle "large" ints with your LargeInt class by somehow storing them in an array of ints. That's okay, BigInteger also works like that (more or less), but you cannot just do calculations by trying to convert back to int (after all those numbers are too big for int arithmetic to handle, even if you do the string parsing properly).

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

1 Comment

int cannot handle these big numbers. You need to implement the addition yourself somehow. That's really quite a task. Maybe take a look at the source for BigInteger (I am assuming this is a school assignment. In real life, you would just use BigInteger directly).

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.