Note that there are some explanatory texts on larger screens.

plurals
  1. POReading values to array, inside a loop
    primarykey
    data
    text
    <p>I am trying to build a program which allows a user to input an actors name and details (age+address) and also 2 films they've starred in. These films must be read into a main array in the main method, but each individual actors films must be copied into a designated array in my actor class to store the actors films individually.</p> <p>I am currently trying to read the values into my array inside a loop in my main method: </p> <pre><code>import java.util.Scanner; public class main { public static void main(String[] args) { Scanner kbd = new Scanner (System.in); String code=""; System.out.println("How many actors would you like to enter?"); int amt = kbd.nextInt(); int noOfFilms = (amt*2); Actor [] arrayOfActors = new Actor[amt]; //Array of ALL films, each actors films must be copied to seperate array in the actor class. String [] allFilms = new String[noOfFilms]; kbd.nextLine(); int count = 1; int i = 0; do { count++; System.out.println("Enter the Details for actor "+(count-1)+"\n"); System.out.println("Enter actor name:"+"\n"); String name = kbd.nextLine(); System.out.println("Enter actor age:"+"\n"); int age = kbd.nextInt(); kbd.nextLine(); System.out.println("Enter actor address:"+"\n"); String address = kbd.nextLine(); //Read in the actors films System.out.println("Enter film 1 for "+name+"\n"); String film1 = kbd.nextLine(); allFilms[i] = film1; System.out.println("Enter film 2 for "+name+"\n"); String film2 = kbd.nextLine(); allFilms[i+1] = film2; //Create an actor as array is full of references only. arrayOfActors[i] = new Actor(name, address, age); i++; arrayOfActors[i-1].print(); } while (count &lt;= amt); System.out.println("This was in the films array: "+allFilms[1]); } } </code></pre> <p>Obviously the way I have it structured currently will not work as every time the loop starts the values will just be over-written and the only details stored will be the last actors films to be entered.</p> <p>I am stuck trying to work around this and read in all the films, which will then need to be deep copied into another array. (in Actor class) </p> <p>This is a college assignment and must be done this way. Any suggestions would be of great help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload