N. Sweeney N. Sweeney - 29 days ago 12
Java Question

java.util.NoSuchElementException in main method using Scanner w/ input & output text files

Error:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at PetOrganizer.main(PetOrganizer.java:23)


This is the main class which interacts with the the Pet Class which is also posted below. First time using FileWriter so if I'm doing something blantantly wrong here just let me know. Line 23 of my code for the main class is the like with "String name".

Edit: Added the input.txt file there is also an output.txt file that the program is meant to print to but it's empty

Code:

import java.util.Scanner;
import java.io.*;

public class PetOrganizer {

public static void main(String[] args) throws IOException
{

//Intialize Text File along with Writer variables
File TextFile = new File("input.txt");
File OutputFile = new File("output.txt");
FileWriter fWriter = new FileWriter(TextFile, true);
FileWriter fOWriter = new FileWriter(OutputFile);
PrintWriter pWriter = new PrintWriter(fOWriter);


//Initialize Scanner Variable
Scanner in = new Scanner(TextFile);


//Gets input from user for pet #1
System.out.println("Please enter what you would like your first pet to be named: ");


This is cut in the code is to show line 23 which is causing the error seen above

String name = in.nextLine();

System.out.println("Please enter how old you would like "+name+" to be:");
int age = in.nextInt();

System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
String pettype = in.nextLine();

System.out.println("Please enter the name of "+name+"'s owner");
String owner = in.nextLine();

//Creates first instance of Pet and uses input from user
Pet p1 = new Pet();
p1.setPetAge(age);
p1.setPetName(name);
p1.setPetType(pettype);
p1.setPetOwner(owner);

//Gets input from user for pet #1
System.out.println("Please enter what you would like your second pet to be named: ");
name = in.nextLine();

System.out.println("Please enter how old you would like "+name+" to be:");
age = in.nextInt();

System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
pettype = in.nextLine();

System.out.println("Please enter the name of "+name+"'s owner");
owner = in.nextLine();

//Creates first instance of Pet and uses input from user
Pet p2 = new Pet();
p2.setPetAge(age);
p2.setPetName(name);
p2.setPetType(pettype);
p2.setPetOwner(owner);

//Gets input from user for pet #1
System.out.println("Please enter what you would like your third pet to be named: ");
name = in.nextLine();

System.out.println("Please enter how old you would like "+name+" to be:");
age = in.nextInt();

System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
pettype = in.nextLine();

System.out.println("Please enter the name of "+name+"'s owner");
owner = in.nextLine();

//Creates first instance of Pet and uses input from user
Pet p3 = new Pet();
p3.setPetAge(age);
p3.setPetName(name);
p3.setPetType(pettype);
p3.setPetOwner(owner);

//Outputs all of the instances of Pet with the info gathered from user input
pWriter.println("/////////////////////////////////////////////////////////");
pWriter.println("///////////////////PATIENT LIST//////////////////////////");
pWriter.println("Pet 1");
pWriter.println("Pet 1 Age: "+ p1.getPetAge());
pWriter.println("Pet 1 Name: "+p1.getPetName());
pWriter.println("Pet 1 Type: "+p1.getPetType());
pWriter.println("Pet 1 Owner: "+p1.getPetOwner());
pWriter.println("Pet 2");
pWriter.println("Pet 2 Age: "+ p2.getPetAge());
pWriter.println("Pet 2 Name: "+p2.getPetName());
pWriter.println("Pet 2 Type: "+p2.getPetType());
pWriter.println("Pet 2 Owner: "+p2.getPetOwner());
pWriter.println("Pet 3");
pWriter.println("Pet 3 Age: "+ p3.getPetAge());
pWriter.println("Pet 3 Name: "+p3.getPetName());
pWriter.println("Pet 3 Type: "+p3.getPetType());
pWriter.println("Pet 3 Owner: "+p3.getPetOwner());
pWriter.println("//////////////////////////////////////////////////////////");
pWriter.println("//////////////////////////////////////////////////////////");

fWriter.close();
pWriter.close();
in.close();

}


}

Pet Class Code:

public class Pet {
//Class Private Data Members
private String petName;
private int petAge;
private String petType; //Cat, Dog, Ferret, etc
private String petOwner;

//Constructor
public Pet()
{

}

//Public Methods
public String getPetName() {
return petName;
}

public void setPetName(String name) {
petName = name;
}

public int getPetAge() {
return petAge;
}

public void setPetAge(int age) {
petAge = age;
}

public String getPetType() {
return petType;
}

public void setPetType(String type) {
petType = type;
}

public String getPetOwner() {
return petOwner;
}

public void setPetOwner(String owner) {
petOwner = owner;
}


}

Input File:

George
18
Dog
Ben
Fred
19
Ferret
Carl
Steve
20
Cat
Dog

Answer

If you need to use a file, using bufferedReader you can go through a text file, line by line until it is finished. For example if you had a text file that had

Dog1

Dog2

the first time it goes through the while loop, the string currentWord is "Dog1" the second time it goes through the while loop, the string currentWord is "Dog2" etc

public static void main(String[] args) 
    {
    try (BufferedReader br = new BufferedReader(new FileReader("C:\\words.txt")))
        {
            String currentWord;
            while ((currentWord = br.readLine()) != null)
            {
               //USE EACH OF YOUR STRINGS HERE i.e String dogName = currentWord
            }
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

"C:\words.txt" refers to the location of the file you want to use. to use this just import

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;