#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    JAVA program won't print


    I am in my first semester of Java, which is my first semester of programming EVER and my mistake is that I'm taking it online. So....please be patient because I am a struggling newbie.

    My last two programs I have written will not produce output and I'm not sure why. I am getting a message that says my import statement is NOT being used.

    Here is the code:

    import java.util.Scanner;

    public class ElementSwap
    {
    public static void main(String[] args)
    {
    /**
    * @param numbers the array
    * @param temp temporary holding place for an element
    * @return
    */
    double[] numbers = {1, 2, 3, 4, 5};
    {
    System.out.print(" | ");
    System.out.print(numbers[5]);
    }
    {
    double temp = numbers[0];
    numbers[0] = numbers[5];
    numbers[5] = temp;

    }
    System.out.print(" | ");
    System.out.print(numbers[5]);
    {

    }

    }
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    One problem you have with your array is the index.

    In any array, the first element is at position 0. So in a 5 element array, the last valid position is 4.

    When you try to print numbers[5], you are probably getting an ArrayIndexOutOfBoundsException.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Thanks, Bullet. I changed that to 4. My goal is to have all 5 of the elements print. Beforehand so that it shows the original numbers, then aftewards so that it shows the numbers after the swap.

    My print statement is:
    System.out.print(numbers[5]);
    Is that right?

    Also, it isn't giving me ANY errors so I have no clue what needs to be fixed.


    Originally Posted by bullet
    One problem you have with your array is the index.

    In any array, the first element is at position 0. So in a 5 element array, the last valid position is 4.

    When you try to print numbers[5], you are probably getting an ArrayIndexOutOfBoundsException.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    For an easy way to see the contents of an array use this method:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    The ArrayIndexOutOfBoundsException only occurs during runtime, not compile-time. So the compiler won't catch it.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Thanks NormR. I want to print all the elements, separated by a |. I tried using the method you provided but still didn't get any output.

    Originally Posted by NormR
    For an easy way to see the contents of an array use this method:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    The method will print if it is executed. Do you get errors? Copy the full text of the error messages and paste them here.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    NormanR, YOU are awesome!!! I wasn't getting any error messages, however, I did notice my package info was completely gone! So, I corrected that and added your print statement......which worked BEAUTIFULLY!!!!

    Thank you SO much!!!

    Originally Posted by NormR
    The method will print if it is executed. Do you get errors? Copy the full text of the error messages and paste them here.

IMN logo majestic logo threadwatch logo seochat tools logo