Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is my code working

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Is my code working

    Hello all, I am on a computer that I cannot download anything on and it does not have jdk on it so I cannot test my code to see if it is running. Anyone mind running it for me to see if all is right? I believe it is, but I could be wrong. What my code does is add two integers of up to 50 digits each together.
    import java.util.* ; 
     
      public class Calculator {
        public static void main(String[] args) {   
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first integer:");
        String a = s.nextLine(); 
        System.out.print("Enter the second integer:");
        String b = s.nextLine();    
        int[] num1 = new int[a.length()];
        int[] num2 = new int[b.length()];
        for (int i=0;i<a.length();i++) {
          num1[i] = (int) a.charAt(i);
          num2[i] = (int) b.charAt(i);
        }
        int[] sum = new int[a.length()];
        for (int n=b.length()-1;n>=0;n--)
        {
          if (num1[n]+num2[n]<10)
          {
            sum[n]=num1[n]+num2[n];
     
          }  
          if (num1[n]+num2[n]>=10) 
          {
            sum[n]=(num1[n]+num2[n])%10;
            sum[n-1]=sum[n-1]+1;  
          }
        }
        for (int d=0; d<a.length();d++) {
          System.out.print(sum[d]+" ");
        }  
      }
    }
    Thanks for any help!


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Is my code working

     

    Enter the first integer: 9
    Enter the second integer: 2
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Calculator.main(Calculator.java:27)




    It points to this line: sum[n-1]=sum[n-1]+1;

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    return0 (October 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Is my code working

    Thanks for doing that I really appreciate it. I'm not sure what is wrong with that line of code I wrote though. Anybody know what I'm doing wrong here?
    sum[n-1]=sum[n-1]+1;


    --- Update ---

    I looked up the error online but still am not grasping the meaning of it.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Is my code working

    ArrayIndexOutOfBoundsException
    Here's code that will get that error:
    int[] intA = new int[2];  // has 2 elements: valid indexes are: 0 and 1
    intA[3] =  4;  //  ERROR:  3 is out of bounds
    intA[-1] = 55;  // ERROR: -1 is out of bounds
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    return0 (October 29th, 2013)

  7. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Is my code working

    So would that code example you showed me compile like my program did then show an error? How come it lets my program compile and lets the user enter the integers before showing the error?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Is my code working

    Yes, it would compile without any errors. Maybe a really good compiler could look at the code and give an error or warning, but none that I know of. Normally code uses a variable as an index instead of hardcoding values like above:
      int[] intA = new int[2];  // has 2 elements: valid indexes are: 0 and 1
      // ...
      int ix = 3;    // will be index that is out of bounds
      // then many statements later
     
       intA[ix] =  4;  //  ERROR:  3 is out of bounds

    How come it lets my program compile and lets the user enter the integers before showing the error?
    That's the way programs work. It is up to the programmer to write code to detect when an index is out of bounds and not use it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help i cant get my code working
    By alexrox27 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 23rd, 2013, 11:27 AM
  2. Code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 23rd, 2013, 02:12 PM
  3. Please why is this code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2013, 10:18 AM
  4. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  5. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM