Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: File input using Scanner help.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File input using Scanner help.

    Ok so i have an assignment I'm working on where i have to write a program that has to:

    1. Read the elements of a book(id, title, isbn, author, genre) from a text file.
    2. Place these elements into a Book object.
    3. Place these Book objects into a Book type array.
    4. Prompt user to enter a an id for a Book and program should reply with all information about the book(ex: User enters "12345" and program should print to screen: Id: 12345, Title: Jimmy Cracked Corn, ISBN: 124512345, Author: JohnDoe, Genre: Fiction)

    Now i am stuck on the part where i have to read in more and place more than one book object into the Book array. Here is my code so far.

    package assg2_Harris;
     
    import java.util.*;
    import java.io.*;
     
    public class FileInput extends Book{
     
    public static void main(String[] args) {
     
    	Scanner inStream = null;
     
    	Book[] Catalog = new Book[100];
    	int count = 0;
     
    	try
    	{
    		inStream = new Scanner(new File("catalog.txt"));
    	}
    	catch(FileNotFoundException x)
    	{
    		System.out.println("File not found.");
    	}	
    	while(inStream.hasNextLine()){	
    		Catalog[count] = new Book(inStream.nextInt(),inStream.next(),inStream.nextInt(),inStream.next(),inStream.next());
    		System.out.println(Catalog[count]);
    		count++;
    	}
    	inStream.close(); 
            System.out.println(Catalog[count]);
    	}
    }
    Here is my code for the book class also:
    package assg2_Harris;
     
    import java.util.*;
     
    import java.io.*;
     
    public class Book {
     
    	private int bookid;
    	private String bookname;
    	private int isbn;
    	private String author;
    	private String genre;
     
    	public Book(){
    		bookid = 0;
    		bookname = "";
    		isbn = 0;
    		author = "";
    		genre = "";
    	}
    	public Book(int id, String title, int i, String name, String genre){
    		bookid = id;
    		bookname = title;
    		isbn = i;
    		author = name;
    		this.genre = genre ;
    	}
     
    	public int getBookId(){
    		return  bookid;
    	}
     
    	public String getBookname() {
    		return bookname;
    	}
     
    	public int getIsbn() {
    		return isbn;
    	}
     
    	public String getAuthor() {
    		return author;
    	}
     
    	public String getGenre() {
    		return genre;
    	}
     
    	public String toString(){
    		return ("Book id: " + bookid + ", Title: " + bookname + ", ISBN: " 
    				+ isbn + ", Author: " 
    				+ author + ", Genre: " + genre); 
    	}
    And also my txt file looks like this:

    12345 Emma 0111111111 Rowling Fiction
    15678 Timmy 135378564 Jesus NonFiction

    Now i'm having a problem with this line file input class:

    Catalog[count] = new Book(inStream.nextInt(),inStream.next(),inStream.n extInt(),inStream.next(),inStream.next());

    The print statement is under it to see whats going on inside the array.

    Anyway when I try to run my program this is what happens:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at assg2_Harris.FileInput.main(FileInput.java:24)

    Book id: 12345, Title: Emma, ISBN: 111111111, Author: Rowling, Genre: Fiction.

    Please help i have no idea why the lines inside the while loop aren't working for what i want to do....

    Here's the link to my assignment prompt if you want to take a look at that.
    https://blackboard.ecu.edu/bbcswebda...ssignment2.pdf


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: File input using Scanner help.

    Place your code inside code tags for formatting and readability.

    [C0DE=java]
    <code>
    [/CODE]

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File input using Scanner help.

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at assg2_Harris.FileInput.main(FileInput.java:24)
    When the nextInt() method is called on line 24, there is no data available to be read.
    Make sure that the file being read has all the data that the program is trying to read.
    The Scanner class has methods whose names start with hasNext that can be used to test if there is data available before calling the method to read the data.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File input using Scanner help.

    What do you mean by there's no data available to read? I thought each next method gets a specific piece of information from my text file and stores into into the variables in my Book object. I can get it to work for one Book object if i take it out of the while loop but as soon as i put a while loop around it i get the error.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File input using Scanner help.

    Print out all data as it is read from the file. When you get the error message you will know where the program was when it tried to read and got the error: NoSuchElementException
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File input using Scanner help.

    How would i go about printing it out as it's read in since it's only being read in parameters?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File input using Scanner help.

    Change the code to read into variables, print the values of the variables and use those variables as args to the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. keyboard scanner to take users input and enter students information method
    By foresboo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 7th, 2013, 04:39 PM
  2. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  3. Replies: 5
    Last Post: October 19th, 2011, 08:21 PM
  4. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM