Jump to content

Java: Program produces console output, but has run time error?

AjAlien
Go to solution Solved by 79wjd,

Don't reinitialize numElem.

 

You have an array out of bounds an exception (i.e. you're accessing an index that doesn't exist).

 

(I'm assuming there are potentially other test cases)

 

Try putting in some print statements and you'll see exactly where the exception is occurring. 

Hello,

 

I am having issues figuring out why this program is experiencing a run-time error but is producing a correct console output. I've been stuck on it for almost an hour.

 

I'd appreciate the help.

 

Code:

import java.util.Scanner;
public class ModifyArray {
	// Can't modify code above this
   public static void swapArrayEnds(int[] sortArray, int numElem)
   {
      numElem = 4;
      int i = 0;
      int tmpStore = 0;
      tmpStore = sortArray[0];
      sortArray[0] = sortArray[numElem - 1];
      sortArray[numElem - 1] = tmpStore;
   }
   // Can't modify code below this
   public static void main (String [] args) {
      int numElem = 4;
      int[] sortArray = new int[numElem];
      int i = 0;
      sortArray[0] = 10;
      sortArray[1] = 20;
      sortArray[2] = 30;
      sortArray[3] = 40;
      swapArrayEnds(sortArray, numElem);
      for (i = 0; i < numElem; ++i) {
         System.out.print(sortArray[i]);
         System.out.print(" ");
      }
      System.out.println("");
      return;
   }
}

Output:

 

Capture.PNG

CPU - I7 4770k @ 4.3GHz GPU - EVGA Nvidia GeForce GTX 780 Motherboard - ASUS Maximus VI Extreme RAM - 16GB of Corsair Dominator Platinum @ 2133MHz  Case - Corsair 800D PSU - Corsair AX1,200i CPU Cooler - Custom Liquid CPU Cooler SSD - Samsung 840 Pro 256GB Hard Drive - Western Digital Black 1TB.

Link to comment
Share on other sites

Link to post
Share on other sites

Don't reinitialize numElem.

 

You have an array out of bounds an exception (i.e. you're accessing an index that doesn't exist).

 

(I'm assuming there are potentially other test cases)

 

Try putting in some print statements and you'll see exactly where the exception is occurring. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't see a runtime exception when I run this code and I don't see where one would happen.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, djdwosk97 said:

Don't reinitialize numElem.

 

You have an array out of bounds an exception (i.e. you're accessing an index that doesn't exist).

 

(I'm assuming there are potentially other test cases)

 

Try putting in some print statements and you'll see exactly where the exception is occurring. 

Indeed there are other test cases, the eBook which has the exercises doesn't show them though (really not a fan of this eBook). Thank you though, your answer made a lot of sense and helped a great deal!

CPU - I7 4770k @ 4.3GHz GPU - EVGA Nvidia GeForce GTX 780 Motherboard - ASUS Maximus VI Extreme RAM - 16GB of Corsair Dominator Platinum @ 2133MHz  Case - Corsair 800D PSU - Corsair AX1,200i CPU Cooler - Custom Liquid CPU Cooler SSD - Samsung 840 Pro 256GB Hard Drive - Western Digital Black 1TB.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, AjAlien said:

(really not a fan of this eBook).

Yeah, neither am I. ++i is stupid. 

 

The main method also has a few interesting....quirks....in the way it's written that just add to the confusion. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×