我试图得到它,以便当用户输入例如吉他模型时,它将从我的ArrayList
返回该特定模型的所有细节。但是,当我运行程序时没有任何结果,我没有错误。我不知道该怎么做,任何帮助将不胜感激。
P.S。在我的交换机案例中,我一直尝试多种不同的方式来获得没有运气的结果,所以如果它到处都是请忽略。
package guitarsrentalmanagementsystem;
import StockManagement.Guitar;
import java.util.ArrayList;
import java.util.Scanner;
public class GuitarsRentalManagementSystem {
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Guitar> data = new ArrayList<>();
data.add(new Guitar(100, 150.99, "Gibson", "Les Paul",
"Acoustic", "INDIAN_ROSEWOOD", "MAHOGANY", 1995, 8.99, "Red",
true));
data.add(new Guitar(101, 180.00, "Fender", "Stratocaster",
"Electric", "BRAZILIAN_ROSEWOOD", "MAHOGANY", 2001, 10.99, "Brown",
true));
data.add(new Guitar(102, 110.50, "Martin", "Noisemaker",
"Acoustic", "INDIAN_ROSEWOOD", "BRAZILIAN_ROSEWOOD", 2005, 5.99, "Black",
true));
for(Guitar g : data){
g.printDetail();
}
//Show data from the adobe arraylist
System.out.println("Valid search parameters are as follows :");
System.out.println("1. Model, eg Les Paul");
System.out.println("2. Serial number, eg 101");
System.out.println("3. Year of manufacture, eg 2001");
Scanner input = new Scanner(System.in);
System.out.println("Please select how you would like to search");
int search = input.nextInt();
for(Guitar g : data){
switch(search){
case 1:
System.out.println("You have chosen to search by model");
System.out.println("Valid models are Les Paul, Stratocaster or Noisemaker");
System.out.println("Please enter a model to search");
Scanner inputO1 = new Scanner(System.in);
String modelSearch = input.next();
if("Les Paul".equals(modelSearch)){
g.printDetail();
}
break;
case 2:
System.out.println("You have chosen to search by Serial number");
System.out.println("valid serial numbers are between 100 and 105");
System.out.println("Please enter a serial number to search");
Scanner inputO2 = new Scanner(System.in);
int serialSearch = input.nextInt();
System.out.println(g.getSerialNumber());
break;
case 3:
System.out.println("You have chosen to search by year of manufacture");
System.out.println("Valid manufacture dates are 1995, 2001 and 2005");
System.out.println("Please enter a manufacture year to search");
Scanner input03 = new Scanner(System.in);
int yearSearch = input.nextInt();
System.out.println(g.getYearOfManufacture());
break;
}
break;
}
}
}
答案 0 :(得分:1)
您正在阅读输入的方式存在问题:当您使用scanner对象并在其上调用nextInt()时。它只读取数字。同样,当你接下来打电话时,它只读取一个字符串值,即
如果您输入“Les Paul”作为输入,则扫描仪对象next()方法仅准备Les,因此它与您的条件不符。
所以一旦你读取整数调用nextLine()方法,那么光标移动到下一行,现在通过再次调用nextLine()方法从下一行读取输入
case 1:
System.out.println("You have chosen to search by model");
System.out.println("Valid models are Les Paul, Stratocaster or Noisemaker");
System.out.println("Please enter a model to search");
Scanner inputO1 = new Scanner(System.in);
input.nextLine();
String modelSearch = input.nextLine();
if ("Les Paul".equals(modelSearch)) {
g.printDetail();
}
break;
如果用户将模型输入为“Les Paul”,则上述代码将起作用。