如何在java arraylist中放入任意个对象,在哪个循环里面有报错谁帮我改一下
代码如下importjava.io.File;importjava.util.ArrayList;importjava.util.Collections;importja...
代码如下
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class main {
public static void main(String[]args){
String data;
String title;
String type;
Double rank;
String f;
Scanner scan = new Scanner(System.in);//read from user
ArrayList<Movie> list=new ArrayList<Movie>();
try{//read from file
System.out.print("What is your file's movies");
data=scan.nextLine();
Scanner reader= new Scanner(data);//Create scanner to read from file
while(reader.hasNext()){
System.out.print(title=reader.nextLine());
System.out.print(type =reader.nextLine());
System.out.print(rank=reader.nextDouble());
Movie mo1=new Movie(title, type, rank);
list.add(mo1);
//list.add(new (Movie(title,type,rank)));
}
}catch(Exception e){
e.printStackTrace();
}
//Collections.sort(list);
System.out.println("Which movie you wanna see the review");
if((f = scan.nextLine())=="exit") {
}
else {
}
}
public class Movie{//movies information
String title;
String type;
Double rating;
public Movie(String title,String type,Double rating)//Constructor
{
this.title=title;
this.type=type;
this.rating=rating;
}
}
} 展开
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class main {
public static void main(String[]args){
String data;
String title;
String type;
Double rank;
String f;
Scanner scan = new Scanner(System.in);//read from user
ArrayList<Movie> list=new ArrayList<Movie>();
try{//read from file
System.out.print("What is your file's movies");
data=scan.nextLine();
Scanner reader= new Scanner(data);//Create scanner to read from file
while(reader.hasNext()){
System.out.print(title=reader.nextLine());
System.out.print(type =reader.nextLine());
System.out.print(rank=reader.nextDouble());
Movie mo1=new Movie(title, type, rank);
list.add(mo1);
//list.add(new (Movie(title,type,rank)));
}
}catch(Exception e){
e.printStackTrace();
}
//Collections.sort(list);
System.out.println("Which movie you wanna see the review");
if((f = scan.nextLine())=="exit") {
}
else {
}
}
public class Movie{//movies information
String title;
String type;
Double rating;
public Movie(String title,String type,Double rating)//Constructor
{
this.title=title;
this.type=type;
this.rating=rating;
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询