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 5 of 5

Thread: Array, only first item is being changed

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Array, only first item is being changed

    I've got two arrays. One that you input words into, and one that changes from Yes to No. When you put the word into the array using an input string, it automatically assigns its counterpart in the second array to No.

    You can then change No to Yes but inputting "Word + Yes". And it should print this out into the terminal. However, it is only working for one of the words. I'm testing with two currently.

    I'm using i to point to an element in the array.

    The way my code is set out is that there are For Loops nested within each other. I can tell that part of it is working as it works successfully for the first item but I'm not sure why only the first item is being affected but the rest won't change. I'm hoping I could get some help:

    This is my code

    for (i=0; i<arrayWord.length; i++) {
    if inputString.Contains(arrayWord[i] + "yes") {
        arrayYesNo[i] = "Yes"
    }
    for (i=0; i<arrayWord.length; i++) {
    system.out.println(arrayWord[i] + arrayYesNo[I]);
    }
    } // Close Second For Loop
    } // Close First For Loop

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array, only first item is being changed

    Can you post the input to the above posted code (What is in arrayWord) and also what it prints out so we can see what you are seeing?
    Add some comments where the output is not what you expect.

    Where is the variable I declared and given a value? What value is in it when the code executes?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array, only first item is being changed

    Quote Originally Posted by Norm View Post
    Can you post the input to the above posted code (What is in arrayWord) and also what it prints out so we can see what you are seeing?
    Add some comments where the output is not what you expect.

    Where is the variable I declared and given a value? What value is in it when the code executes?
    As I said above, arrayWord contains words that are assigned to the array during the runtime through an Input String.
    What it prints out first is Word - No.

    I should then be able to type in the word and add Yes after it and get the output: "Word - Yes".
    It is working for the first word in the array but if I try any words after that it just doesn't want to work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array, only first item is being changed

    Can you post the actual values being read by the program from the array
    and what the program prints out?

    Also what about my question re the I variable?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array, only first item is being changed

    Don't worry. I got it working.

Similar Threads

  1. I don't get the concept of how to insert an item into an array; exercise 170
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 5th, 2014, 11:15 AM
  2. How to get a random item from an array?
    By zhider in forum Java Theory & Questions
    Replies: 2
    Last Post: December 15th, 2012, 09:13 PM
  3. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM