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: problem with Array (Java Beginner)

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question problem with Array (Java Beginner)

    Hi guys,

    I have a problem with my code and I really hope you can help me! In this part of my code I would like to let the user make two user inputs. "eingabeZeile" and "eingabeSpalte". This user input should then be but to the place of my array "Spielfeld" like this: spielfeld[eingabeZeile][eingabeSpalte], but in the way I try to use it eclipse just gives me an error code: The left-hand side of an assignment must be a variable.
    Unbenannt2.JPGUnbenannt2.JPGUnbenannt.JPG

    I use IO.readInt() in there to let the user type in an input. Our prof gave us that class.
    I hope someone can tell me what I'm doing wrong or how I can solve this different. As you might have guessed: I'm not an english native, so feel free to correct, but not to judge and please ask if something is unclear.

    Thank you !
    Joana

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: problem with Array (Java Beginner)

    In the future. please place your code between code tags to format your code. An image is hard to read. Check out the BB Codes at the bottom of this page for more info.

    Edit: If you go into the advanced editor (Go Advanced), you can then highlight your code and click on #. That will place code tags around you code for your.

    Regarding your problem. I presume you want to allocate an array based on the two values you entered. You need to do it like this:

    int[][] Spielfeld = new int[eingabeZeile][eingabeSpalte];

    That creates a new 2-D array of eingableZeile rows by eingabeSpalte columns. And remember that Java does not really have 2-D arrays. It is simply an array of arrays.

    Regards,
    Jim

  3. #3

    Default Re: problem with Array (Java Beginner)

    Quote Originally Posted by jim829 View Post
    In the future. please place your code between code tags to format your code. An image is hard to read. Check out the BB Codes at the bottom of this page for more info.

    Edit: If you go into the advanced editor (Go Advanced), you can then highlight your code and click on #. That will place code tags around you code for your.

    Regarding your problem. I presume you want to allocate an array based on the two values you entered. You need to do it like this:

    int[][] Spielfeld = new int[eingabeZeile][eingabeSpalte];

    That creates a new 2-D array of eingableZeile rows by eingabeSpalte columns. And remember that Java does not really have 2-D arrays. It is simply an array of arrays.

    Regards,
    Jim
    Here is the coder for Equality of Two Arrays
    public class Exercise23 {
    static void equality_checking_two_arrays(int[] my_array1, int[] my_array2)
    {
    boolean equalOrNot = true;

    if(my_array1.length == my_array2.length)
    {
    for (int i = 0; i < my_array1.length; i++)
    {
    if(my_array1[i] != my_array2[i])
    {
    equalOrNot = false;
    }
    }
    }
    else
    {
    equalOrNot = false;
    }

    if (equalOrNot)
    {
    System.out.println("Two arrays are equal.");
    }
    else
    {
    System.out.println("Two arrays are not equal.");
    }
    }

    public static void main(String[] args)
    {
    int[] array1 = {2, 5, 7, 9, 11};
    int[] array2 = {2, 5, 7, 8, 11};
    int[] array3 = {2, 5, 7, 9, 11};

    equality_checking_two_arrays(array1, array2);
    equality_checking_two_arrays(array1, array3);
    }
    }


    Note: If there is any mistake please correct it and mention the error.
    Share your ideas also

  4. #4
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with Array (Java Beginner)

    Hi,
    Thanks for your answers, but my problem is not exactly solved by this. What I like todo ist to have an array and then the user can say which "field" of the Array he wants to change. So by typing in 3 and 2 the user would for example reach spielfeld[3][2]... I'm sorry for not specifying that...
    Thanks
    Joana

  5. #5

    Default Re: problem with Array (Java Beginner)

    Quote Originally Posted by indirekt29 View Post
    Hi,
    Thanks for your answers, but my problem is not exactly solved by this. What I like todo ist to have an array and then the user can say which "field" of the Array he wants to change. So by typing in 3 and 2 the user would for example reach spielfeld[3][2]... I'm sorry for not specifying that...
    Thanks
    Joana
    Ok I can understand but I don't know Sorry

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: problem with Array (Java Beginner)

    Please don't provide solutions (especially incorrect ones or ones that don't address the problem). It's okay to help with syntax but folks won't learn anything if they are provided solutions to problems.

    Regards,
    Jim

    --- Update ---

    The syntax for assigning an item to a specific place in an array is as follows.

    array[1][2] = item;

    Regards,
    Jim

Similar Threads

  1. Beginner java problem
    By kinkita in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 30th, 2013, 03:44 PM
  2. Beginner Problem: Array sorting decreasing element values? Why?
    By Hatsman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2013, 02:37 PM
  3. Java Array Merge Input Beginner Question
    By grumpy_int05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 27th, 2013, 04:03 PM
  4. Replies: 2
    Last Post: August 30th, 2012, 03:26 AM
  5. Beginner Array Problem. Swapper
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 4th, 2012, 09:32 PM

Tags for this Thread