Java 在main中使用引用变量调用方法
我试图在包含其他方法的循环之外调用一个方法,在我的main中调用strList ArrayList变量,但是当我这样做时,netbeans说它找不到符号。我已经从其他方法调用了一些方法,但是main给了我一些问题。有什么我不知道的吗?如果你愿意,谢谢你的帮助 我发行的前一期:writeList(strList) 节目Java 在main中使用引用变量调用方法,java,methods,Java,Methods,我试图在包含其他方法的循环之外调用一个方法,在我的main中调用strList ArrayList变量,但是当我这样做时,netbeans说它找不到符号。我已经从其他方法调用了一些方法,但是main给了我一些问题。有什么我不知道的吗?如果你愿意,谢谢你的帮助 我发行的前一期:writeList(strList) 节目 public static void main(String[] args) throws FileNotFoundException, IOException {
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
boolean shouldContinue = true;
while (shouldContinue == true) {
nameInput();
Scanner input = new Scanner(System.in);
shouldContinue = promptForContinue(input);
}
writeList(strList);
}
/**
*
* @param name
*/
public static void nameInput() throws FileNotFoundException, IOException {
System.out.println("What is the name of the cartoon character : ");
Scanner keyboard = new Scanner(System.in);
CartoonStar star = new CartoonStar();
String name = keyboard.next();
star.setName(name);
typeInput(keyboard, star);
}
public static void typeInput(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {
System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
+ "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");
boolean again = true;
while (again) {
try {
input = keyboard.nextInt();
} catch (Exception e) {
System.out.println("Error :invalid input ");
again = true;
keyboard.next();
}
if (input > 0 && input <= 10) {
again = false;
}
}
switch (input) {
case 1:
star.setType(CartoonType.FOX);
break;
case 2:
star.setType(CartoonType.CHICKEN);
break;
case 3:
star.setType(CartoonType.RABBIT);
break;
case 4:
star.setType(CartoonType.MOUSE);
break;
case 5:
star.setType(CartoonType.DOG);
break;
case 6:
star.setType(CartoonType.CAT);
break;
case 7:
star.setType(CartoonType.BIRD);
break;
case 8:
star.setType(CartoonType.FISH);
break;
case 9:
star.setType(CartoonType.DUCK);
break;
case 10:
star.setType(CartoonType.RAT);
break;
}
popularityNumber(keyboard, star);
}
public static void popularityNumber(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {
System.out.println("What is the cartoon popularity number?");
boolean again = true;
while (again) {
try {
popularity = keyboard.nextInt();
} catch (Exception e) {
System.out.println("Error : invalid input:");
again = true;
keyboard.next();
}
if (popularity >= 0 && popularity <= 10) {
again = false;
}
}
star.setPopularityIndex(popularity);
ArrayList<Object> strList = new ArrayList<Object>();
strList.add(star.getName());
strList.add(star.getType());
strList.add(star.getPopularityIndex());
writeList(strList, keyboard);
}
public static void printList(ArrayList<Object> strList) {
System.out.println(strList);
}
public static void writeList(ArrayList<Object> strList, Scanner keyboard) throws FileNotFoundException, IOException{
System.out.println("Enter the file name");
String fileName = keyboard.next();
PrintWriter writer = new PrintWriter(fileName + ".txt");
System.out.println("Saving. . . . ");
System.out.println("Saved!");
}
//To change body of generated methods, choose Tools | Templates.
private static boolean promptForContinue(final Scanner input) {
boolean isValid = false;
String userInput = "";
do {
System.out.print("Continue (Yes/No):");
userInput = input.next();
isValid = userInput.matches("Yes|No");
// if the input matches yes, ask for the required variables, else break.
if (!isValid) {
System.out.println("\nInvalid entry.");
}
} while (!isValid);
return userInput.equals("Yes") ? true : false;
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException{
//此处的TODO代码应用程序逻辑
布尔值shouldContinue=true;
while(shouldContinue==true){
nameInput();
扫描仪输入=新扫描仪(System.in);
shouldContinue=promptForContinue(输入);
}
写列表(strList);
}
/**
*
*@param name
*/
public static void nameInput()引发FileNotFoundException、IOException{
System.out.println(“卡通人物的名字是什么:”);
扫描仪键盘=新扫描仪(System.in);
CartonStar星形=新的CartonStar();
字符串名称=keyboard.next();
star.setName(name);
打字输入(键盘、星形);
}
公共静态void类型输入(扫描仪键盘、CartonStar星形)抛出FileNotFoundException、IOException{
System.out.println(“卡通人物类型是什么:1=狐狸,2=鸡,3=兔子,4=老鼠,5=狗,\n”
+“6=猫,7=鸟,8=鱼,9=鸭,10=鼠”);
布尔值=真;
同时(再次){
试一试{
输入=键盘.nextInt();
}捕获(例外e){
System.out.println(“错误:输入无效”);
再次=正确;
键盘。下一步();
}
如果(input>0&&input=0&&popularity您的writeList
方法接受两个参数,您只提供一个。另外,我看不出您在哪里定义了作为第一个参数传递的变量strList
。它是静态变量吗,是在您未显示的代码中定义的
看到第二个参数应该是扫描器
,也许这对您有用:
Scanner input = new Scanner(System.in);
while (shouldContinue == true) {
nameInput();
shouldContinue = promptForContinue(input);
}
writeList(strList, input);
将来,您应该提供来自编译器的完整错误消息。让人们猜测错误可能是什么是无效的。您可以共享完整的类文件和相应的错误消息/