+ 1

how to save strings that user input into an array? JAVA

https://code.sololearn.com/cnpMa2W2sCai This is my code so far I asked here before but I still don't get how to save the string into an array. for example, the user puts 3 numbers of characters and input names and role, origin 3 times, 5 times or 10 times. now it displays the string that I already input... Need help you guys I really appreciate any help!!!!

12th Jun 2017, 5:39 AM
Kurt Cobain
Kurt Cobain - avatar
5 Answers
+ 3
try this legendary[] leg = new legendary[numbers]; int i = 0; while (i<numbers) { legendary temp_legend = new legendary(); temp_legend.set_first_name(); temp_legend.set_last_name(); temp_legend.set_nickname(); temp_legend.set_roles(); temp_legend.set_origins(); leg[i]=temp_legend; i++; }
12th Jun 2017, 6:52 AM
Loai Hazima
Loai Hazima - avatar
+ 1
thank you but still doesn't work it gets error .set_first_name(); .set_last_name(); .set_nickname(); .set_roles(); .set_origins(); these parts.. and on for(int y = 0; y < numbers; y++){ leg[y] = temp_legend; } this part temp_legend;
12th Jun 2017, 7:13 AM
Kurt Cobain
Kurt Cobain - avatar
+ 1
I agree with @serena your code is kind of spaghetti , now i am after lunch so i could suggest you this option: import java.util.Scanner; public class main_source { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int numbers = 0; System.out.println("Enter the number of characters you want to put: "); numbers = scan.nextInt(); legendary[] legArr = new legendary[numbers]; int i = 0; String firstName = ""; String lastName = ""; String nickName = ""; String role = ""; String origin = ""; while (i<numbers) { System.out.print("Enter first name: "); firstName = scan.nextLine(); System.out.print("Enter last name: "); lastName = scan.nextLine(); System.out.print("Enter nickname: "); nickName = scan.nextLine(); System.out.print("Enter role: "); role = scan.nextLine(); System.out.print("Enter origin: "); origin = scan.nextLine(); legendary leg = new legendary(firstName,lastName,nickName,role,origin) legArr[i]=leg; i++; } //display for(legendary leg : legArr) { leg.display_info(); } } } //different class import java.util.Scanner; public class legendary { private String firstName; private String lastName; private String nickName; private String role; private String origin; //Constructor public legendary(String firstName,String lastName , String nickName , String role , String origin) { this.firstName=firstName; this.lastName=lastName; this.nickName=nickName; this.role=role; this.origin=origin; } public void display_info() { //This will display first, last, nickname, role, and origin System.out.println("Name: " + firstName + " " + lastName); System.out.println("Nickname: " + nickName); System.out.println("Role: " + role); System.out.println("Origin: " + origin); System.out.print("\n"); }
12th Jun 2017, 10:32 AM
Loai Hazima
Loai Hazima - avatar
+ 1
Thanks a lot guys! you guys so helpful have a good day!
12th Jun 2017, 5:55 PM
Kurt Cobain
Kurt Cobain - avatar