views:

128

answers:

3

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.

Thanks.

+2  A: 

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 Scanner to 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.

Tim Bender
+2  A: 

The exception is being thrown in the call to reader.nextLine() According to the javadoc, this means that nextLine() could not find a next line.

Based on a careful reading of the javadoc, I think that this means that your input file is empty. You could test this by calling hasNextLine() before the call to nextLine().

Stephen C
+1  A: 

Hmmm... I there are two things which are missing here....

Now the NoSuchElementException comes when input is exhausted . It need not to be related to nextLine(). So check whether for the following number of reads

nm = reader.nextLine();
ht = reader.nextInt();
wt = reader.nextInt();
ba = reader.nextLong();
bal = reader.nextDouble();

You have equal number of lines in your inperson.txt

Also When done writing ... do this also..

writer.flush();
writer.close();

Otherwise you won't see any output file... :)

good luck

Favonius
very helpful, thanks!
D. Spigle