Java Beginner Coding Question

Discussion in 'Tech Discussion' started by AMissingLinguist, Oct 20, 2017.

  1. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Here's my code:
    PHP:
    import java.util.Scanner;

    public class 
    ModifyArray {

       public static 
    void swapArrayEnds(int[] sortArrayint numElem) {
          
    int temp 0;
     
          if (
    numElem 0) {
             
    temp sortArray[0];
             
    sortArray[0] = sortArray[numElem];
             
    sortArray[numElem] = temp;
          }
          return;
       }

       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(sortArraynumElem);

          for (
    0numElem; ++i) {
             
    System.out.print(sortArray[i]);
             
    System.out.print(" ");
          }
          
    System.out.println("");

          return;
       }
    }

    My problem is that my method "swapArrayEnds" does not swap the ends and gives me the "Program end never reached. This is commonly due to an invalid memory/array access or divide by 0" text. How do I fix it?

    Edit: Nevermind. I see where I went wrong. Orz. I had to subtract 1 from numElem.
     
  2. Alchyr

    Alchyr Master of Procrastination

    Joined:
    Mar 23, 2016
    Messages:
    644
    Likes Received:
    1,071
    Reading List:
    Link
    as a short tip you can do sortArray.length in the sub to get the number of elements in the array without have to pass it manually
     
    AMissingLinguist likes this.
  3. yorozuyaginchan

    yorozuyaginchan Well-Known Member

    Joined:
    May 26, 2016
    Messages:
    818
    Likes Received:
    715
    Reading List:
    Link
    You should pass (the length of the array-1) to the swapArrayEnds function. sortArray is stored starting from 0 to 3 in this case and the function tries to access the 4th element, which should not exist.
     
    AMissingLinguist likes this.
  4. eksentrysyti

    eksentrysyti Well-Known Member

    Joined:
    Jan 21, 2017
    Messages:
    52
    Likes Received:
    41
    Reading List:
    Link
    Actually, don't even pass the length of the array because it is redundant.

    Instead of:
    public static void swapArrayEnds(int[] sortArray, int numElem)
    or
    public static void swapArrayEnds(int[] sortArray, int sortArray.length)

    Simply use:
    public static void swapArrayEnds(int[] sortArray)

    Then in the body of the function:
    if (sortArray.length > 0)

    ============
    You want to do this because it's less code you need to write and one less variable you need to set (numElem is no longer a variable you need). If you ever need to make changes to your code, it's one less potential thing that can go wrong.
     
    AMissingLinguist likes this.
  5. Ddraig

    Ddraig Frostfire Dragon|Retired lurker|FFF|Loved by RNG

    Joined:
    Apr 6, 2016
    Messages:
    7,855
    Likes Received:
    22,461
    Reading List:
    Link
    Length - 1
     
    AMissingLinguist likes this.