为学校最终项目制作此计划。我试图跑步时遇到错误,不确定我需要做些什么来修复它。 错误说:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at BusinessCardMaker.getName(BusinessCardMaker.java:41)
at BusinessCardMaker.instructions(BusinessCardMaker.java:28)
at BusinessCardMaker.main(BusinessCardMaker.java:131)
我做错了什么。我可以改变什么来使程序更好?我的代码导致错误的错误是什么?
先谢谢你们!
import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class BusinessCardMaker{
private static String wholeName;
private static String firstLineAddress;
private static String secondLineAddress;
private static String company;
private static String location;
private static String jobTitle;
private static int officePhoneNum;
private static String extNum;
private static int input;
private static String email;
private static int cellNum;
private static int cardNumber;
public static void instructions() throws IOException{
System.out.println("Welcome to your very own Business Card Maker");
System.out.println("\nTo access the program please type 1");
Scanner keyboard = new Scanner(System.in);
int startProgram = keyboard.nextInt();
keyboard.close();
if(startProgram == 1)
{
getName();
getAddress();
getDepartment();
getPhoneNums();
getEmail();
generateCardNum();
cardMaker();
}
}
public static void getName()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input your whole name in this format (First M. Last)");
wholeName = keyboard.nextLine();
keyboard.close();
}
public static void getAddress(){
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the first line of your address?");
firstLineAddress = keyboard.nextLine();
System.out.println("What is the second line of your address?");
secondLineAddress = keyboard.nextLine();
keyboard.close();
}
public static void getDepartment(){
Scanner keyboard = new Scanner(System.in);
System.out.println("What company do you work for?");
company = keyboard.nextLine();
System.out.println("What particular location of company do you work for?");
location = keyboard.nextLine();
System.out.println("What is your primary position?");
jobTitle = keyboard.nextLine();
keyboard.close();
}
public static void getPhoneNums()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your cell phone number?");
cellNum = keyboard.nextInt();
System.out.println("What is your office phone number?");
officePhoneNum = keyboard.nextInt();
System.out.println("Do you have and office extention? If yes enter 1, if no enter 0");
input = keyboard.nextInt();
if (input == 1) {
Scanner keyboardExt = new Scanner(System.in);
System.out.println("What is your extention number?");
extNum = keyboardExt.nextLine();
keyboardExt.close();
} else {
extNum = "N/A";
}
keyboard.close();
}
public static void getEmail(){
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your Email Address?");
email = keyboard.nextLine();
keyboard.close();
}
public static void generateCardNum(){
System.out.println("We automatically generated a random business card number");
Random random = new Random();
cardNumber = random.nextInt(1000) + 1;
}
public static void save() throws IOException {
String businessCard = "Business_Card.txt";
File myFile = new File (businessCard);
PrintWriter writer = new PrintWriter(myFile);
System.out.println("Your card has been saved to " + businessCard);
writer.println("\n\n Your Business Card");
writer.println("\n*****************************************************************************");
writer.println("Card Number:" + cardNumber);
writer.println("\n"+company);
writer.println("\n\n\n\n\n "+wholeName);
writer.println(" "+jobTitle);
writer.println("\n\n\n\n\n");
writer.println(location+" "+"Office:"+officePhoneNum+"Ext:"+extNum);
writer.println(firstLineAddress+" "+"Cell:"+cellNum);
writer.println(secondLineAddress+" "+"Email:"+email);
writer.close();
}
public static void checkForFile() throws IOException{
String businessCard = "Business_Card.txt";
File myFile = new File (businessCard);
if(!myFile.exists()){
myFile.createNewFile();
}
}
public static void cardMaker() throws IOException {
System.out.println("\n\n Your Business Card");
System.out.println("\n*****************************************************************************");
System.out.println("Card Number:" + cardNumber);
System.out.println("\n"+company);
System.out.println("\n\n\n\n\n "+wholeName);
System.out.println(" "+jobTitle);
System.out.println("\n\n\n\n\n");
System.out.println(location+" "+"Office:"+officePhoneNum+"Ext:"+extNum);
System.out.println(firstLineAddress+" "+"Cell:"+cellNum);
System.out.println(secondLineAddress+" "+"Email:"+email);
checkForFile();
save();
}
public static void main (String[]args) throws IOException{
instructions();
}
}
答案 0 :(得分:1)
keyboard.hasNextLine()
之前, keyboard.nextLine()
必须返回 true
根据您真正想要实现的目标,您可能希望使用它:
if(keyboard.hasNextLine()) {
String line = keyboard.nextLine();
}
.hasNextLine()
阻止被动等待线路出现(出现新的线条字符),因此下一个解决方案是围绕呼叫
keyboard.nextLine()
使用try / catch。