1

I am trying to copy my instrumentalist array into a new array called copy array and when I go to run it, it just goes blank after I am done the do while loop and doesn't print out the musicians names I have entered. from what I can find and know already I thought this is the right way to copy an array to another. thank you in advanced for your personal input

import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        Scanner keyboard= new Scanner(System.in);
        final int MAXPEOPLE=100;
        instrumentalist [] instrumentalistArr;
        instrumentalistArr=new instrumentalist[MAXPEOPLE];
        char choice;
        int index=0;
        int instrumentalistCOUNT=0;
        do {
            instrumentalist ainstrumentalist = new instrumentalist();
            ainstrumentalist.readin();
            instrumentalistArr[index]=ainstrumentalist;
            instrumentalistCOUNT++;
            index++;
            System.out.println("would you like to enter another data set yes or no");
            choice=keyboard.next().charAt(0); 
        } while(choice!='n');

        instrumentalist [] copyarray;
        copyarray=new instrumentalist[instrumentalistArr.length];

        for(int i=0; i<instrumentalistCOUNT; i++) {
            copyarray[i]=instrumentalistArr[i];
        }

        for(int i=0; index<instrumentalistCOUNT; i++) {
            System.out.println("musician"+" "+ copyarray[index].getmusicianname());
        }
    }
}

1 Answer 1

4

In this for loop:

  for(int i=0; index<instrumentalistCOUNT; i++)
{
    System.out.println("musician"+" "+ copyarray[index].getmusicianname());
}

Change index to i.

So your for loop should be:

for(int i=0; i<instrumentalistCOUNT; i++)
{
    System.out.println("musician"+" "+ copyarray[i].getmusicianname());
}
Sign up to request clarification or add additional context in comments.

1 Comment

oh wow i feel dumb, totally forgot to switch my variables when i made my copyarray thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.