Looking for help with the following code…
package pkgPeople;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class CreateWithoutSerialization {
public static void main(String[] args) throws Exception
{
BankAccount bankAccount = new BankAccount(0, 0);
Person person = new Person();
String nm;
int ht;
int wt;
long ba;
double bal;
File inFile = new File("G:/CS9.27/inperson.txt");
File outFile = new File("G:/CS9.27/outperson.txt");
PrintWriter writer = new PrintWriter(outFile);
Scanner reader = new Scanner(inFile);
nm = reader.nextLine();
ht = reader.nextInt();
wt = reader.nextInt();
ba = reader.nextLong();
bal = reader.nextDouble();
person.setName(nm);
person.setHeight(ht);
person.setWeight(wt);
bankAccount.setAcctID(ba);
bankAccount.setBalance(bal);
System.out.println(person.toString());
//Write the attributes in ASCII to a file
writer.printf("%s is the name of the person.\r\n",nm);
writer.printf("%d inches is the height of %s.\r\n",ht, nm);
writer.printf("%d pounds is the weight of %s\r\n",wt,nm);
writer.printf("%d dollars is the balance of %s\r\n", bal, nm);
writer.printf("%l is the ID of the bank account.\r\n", ba);
}
}
Upon running, i get this exception..
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at pkgPeople.CreateWithoutSerialization.main(CreateWithoutSerialization.java:23)
Is this a file error? Have tried multiple fixes but still stuck.
As a beginner in a programming language, you need to learn how to use the documentation and resources made readily available for that language.
If you look at the javadoc for the method you are using here you would soon realize that the problem is that there is not a new line character for the
Scannerto read in a line. Check your input file and make sure it meets the specifications. If you are sure your input file is correct you can do some debugging by using the file API to make sure the input File exists before attempting to use it as input for the Scanner.All of the information you need is readily available in the javadoc.