Results 1 to 13 of 13
  1. #1
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Two dimensional arrays

    Hi all, I'm new to java and I'm trying to create a two dimensional array:


    public class TwoDimensionalArrays {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    int [][] TwoDim = new int[4][3];

    int temp = 10;

    for(int i = 0; i < 4; i++){
    for(int j = 0; i < 3; j++){

    TwoDim[i][j] = temp;
    temp += 10;

    System.out.print(TwoDim[i][j] + " ");
    }

    }

    }

    }

    I get this error:

    10 20 30 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at TwoDimensionalArrays.main(TwoDimensionalArrays.jav a:15)

    Can someone explain to me what I'm doing wrong? Thanks in advance!

    Gr. Mackenzie

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Two dimensional arrays

    Check the coding of the for() loops for correct usage.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Two dimensional arrays

    Your for loops aren't implemented correctly: For Loops - YouTube

  4. #4
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Two dimensional arrays

    Thanks for the reactions! But I find it strange since I copied it directly from some YT video. It worked on his on his program. There is another thing:

    class IfElseDemo {
    public static void main(String[] args) {

    int testscore = 76;
    char grade;

    if (testscore >= 90) {
    grade = 'A';
    } else if (testscore >= 80) {
    grade = 'B';
    } else if (testscore >= 70) {
    grade = 'C';
    } else if (testscore >= 60) {
    grade = 'D';
    } else {
    grade = 'F';
    }
    System.out.println("Grade = " + grade);
    }
    }

    I copied this from the oracle java tutorials. The output supposed to state a C in this example, but my eclipe didn't have any output. I didn't get an error, but Eclipse didn't print anything out....What's wrong with the above? Thanks in advance!

    Gr. Mackenzie

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Two dimensional arrays

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: Two dimensional arrays

    Quote Originally Posted by mackenzie1235 View Post
    Java Code:
    		for(int i = 0; i < 4; i++){
    			for(int j = 0; i < 3; j++){
    You need glasses; the second loop condition should read 'j < 3' ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Two dimensional arrays

    Java Code:
     class IfElseDemo {
    public static void main(String[] args) {
    
    int testscore = 76;
    char grade;
    
    if (testscore >= 90) {
    grade = 'A';
    } else if (testscore >= 80) {
    grade = 'B';
    } else if (testscore >= 70) {
    grade = 'C';
    } else if (testscore >= 60) {
    grade = 'D';
    } else {
    grade = 'F';
    }
    System.out.println("Grade = " + grade);
    }
    }
    Like this?

    @ Jos, you're right! Haha stupid....

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Two dimensional arrays

    Like this?
    Almost. The code has lost all its formatting. Logically nested statements should be indented. All statements should not start in the first column.

    What happens when the code is compiled and executed?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,008
    Rep Power
    6

    Default Re: Two dimensional arrays

    Quote Originally Posted by mackenzie1235 View Post
    @ Jos, you're right! Haha stupid....
    Not necessarily stupid. You have to wonder why you were ignoring all the hints you were getting. Java is never going to tell you "look buddy, you are doing this wrong in your code and you need to change it to this", that needs to be a product of your own wonderful and clever brain. What Java IS telling you is: the code you're having now is trying to read from an index which does not exist. Hint hint.

    That should trigger you to investigate when that can happen in your code. The "3" that Java tells you is a second big hint if you check your inner loop which was the one that was "stupid".

    Learn from this moment: use the information you get to reason about the problem. Usually the answer will then hit you, or it gives you a point to start adding some System.out statements to get more information from the code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Two dimensional arrays

    @Norm: I just copied it from the Oracle tutorial. Just to see if it worked on my Eclipse. I get no output whatsoever.

    @Gimbal2, I am new to Java. I started learning Java since two weeks now. I try to learn java one hour every day. I still don't have a clear view of the structures and syntax. So those hints I don't understand, that's why I'm asking you...Now I understand, the 3 was referring to j which could have had 4 figures maximum.

    Gr. Mackenzie

    ps. I searched the forum for book topics, but didn't find any. Can you recommend me a java beginner book. A sort of step-by-step introduction to java with exercises and answers provided?
    Last edited by mackenzie1235; 12-16-2013 at 02:58 PM.

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,008
    Rep Power
    6

    Default Re: Two dimensional arrays

    Head First Java 2nd edition is often recommended. It is an older book for Java 5, but it is still relevant. It teaches through puzzles and games rather than in a dry textbook style.

    As to being new: I get it. I'm actually trying to reverse your way of thinking: its not stupid, just "open your mind" to the possibilities that you are currently not seeing yet. Keep at it, eventually it will be easier.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Two dimensional arrays

    I get no output whatsoever.
    The code should print out a message when it executes. If there is no message printed, then either you are not looking in the right place to see the message or the program is not being executed. I get the expected message printed when I compile and execute the program.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Two dimensional arrays

    Thanks the responses! I looked at head first java. I says on the back of book the book isn't for people who have no scripting experience besides HTML or something. I don't have real experience in scripting so could this book be something for me?
    I'm not a very creative person myself and it seems to me you have to be very creative to write good programs so I doubt I will become very good in java, but I will give myself a year and see where I'm at at that time...

    Gr. Mackenzie
    Last edited by mackenzie1235; 12-16-2013 at 04:48 PM.

Similar Threads

  1. 2 dimensional arrays and encryption
    By wdh321 in forum New To Java
    Replies: 0
    Last Post: 08-15-2011, 02:09 PM
  2. Sorting Two Dimensional arrays
    By anfielder in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 07:12 AM
  3. two dimensional arrays
    By cliffh in forum New To Java
    Replies: 2
    Last Post: 11-05-2010, 11:43 PM
  4. dynamic two dimensional arrays?
    By dinosoep in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 06:12 PM
  5. Multi-dimensional arrays
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-15-2009, 08:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •