5 Replies - 973 Views - Last Post: 27 April 2011 - 09:15 PM Rate Topic: -----

#1 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Error (tokenizing) trying to read a txt file...

Posted 26 April 2011 - 08:37 PM

I am trying to write a simple check in check out library application which handles books.
I am given the (books.txt AND cards.txt)
exp of book.txt: the law of power, Tolstoy, 456, false, 0
exp of cards.txt: Jeff johnson, 524-852-0000, 234, 0

I am trying to read the information and display it in the menu; however, I got stuck at line 40 trying to tokenize the info...

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class newinfo {
	public static void main(String[] args, int i) {
	
	// declaring array of 20 cards
	Card[] cards = new Card[20];
	// declare an array of 20 books
	Book[] books = new Book[20];

	// declare a file pointer and open the cards file 
	Scanner fileinput;
        try {
            fileinput = new Scanner(new File("cards.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Error, cards can not be found...");
           return;

        }
	
	// read the next line from the file
	String line = fileinput.nextLine();
        try {
            // declare a file pointer and open the books file
            fileinput = new Scanner(new File("books.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Error, books.txt can not be found...");
           return;

        }
         // tokenize the line to get the data for class variables

        while(fileinput.hasNext())
        {
            String title =  fileinput.next();
            String author = fileinput.next();
            int bookid = fileinput.nextInt();
            books[i] = new Book(tokens[0], tokens[1], parseInt(tokens[2]), false, 0);
            i++;
            }
	// read the next line from the file
	String line = fileinput.nextLine();
        
		// display main menu
	ShowMenu();
	while(command != 0)
	{
	
	
		ShowMenu();
	}


}
}

public static void ShowMenu()
{

        System.out.println();
	System.out.println("     ******  Menu   ********  ");
	System.out.println("  1 - Show all library Books ");
	System.out.println("  2 - Check out a book ");
	System.out.println("  3 - Check in a book ");
	System.out.println("  4 - display this menu ");
	System.out.println();
	System.out.println();


}

public static Card CreateCard(String name, String phone, int CardID, int BookID)
{


}

public class Card {



}

public class Book {



}



Is This A Good Question/Topic? 0
  • +

Replies To: Error (tokenizing) trying to read a txt file...

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error (tokenizing) trying to read a txt file...

Posted 26 April 2011 - 08:46 PM

If he delimiters are always the same, in your case "," a lot easier to use the split() method rather than a Tokenizer:

String line = fileinput.nextLine();
String[] token = line.split(",");

token[0] first string of the line
token[1] second String (after the ,)
....
Was This Post Helpful? 0
  • +
  • -

#3 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Error (tokenizing) trying to read a txt file...

Posted 26 April 2011 - 09:11 PM

Delimiters are always the same.
I replaced the lines according to your suggestion; however, I still have errors @ line 34 38 & 41.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Library {
	public static void main(String[] args, int i) {
	
	// declare an array of 20 cards
	Card[] cards = new Card[20];
	// declare an array of 20 books
	Book[] books = new Book[20];

	// declare a file pointer and open the cards file 
	Scanner fileinput;
        try {
            fileinput = new Scanner(new File("cards.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Program execution ends because the input file cannot be found");
           return;

        }
	
	// read the next line from the file
	String line = fileinput.nextLine();
        try {
            // declare a file pointer and open the books file
            fileinput = new Scanner(new File("books.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Program execution ends because the input file cannot be found");
           return;

        }
	// read the next line from the file
	String line = fileinput.nextLine();
        String[] tokens = line.split(",");
        
	// tokenize the line to get the data for class variables
       books[i] = new Book(tokens[0], tokens[1], parseInt(tokens[2]), false, 0);
         i++;
	// display main menu
	ShowMenu();
	while(command != 0)
	{
		ShowMenu();
	}

}
}

public static void ShowMenu()
{

        System.out.println();
	System.out.println("     ******  Menu   ********  ");
	System.out.println("  1 - Show all library Books ");
	System.out.println("  2 - Check out a book ");
	System.out.println("  3 - Check in a book ");
	System.out.println("  4 - display this menu ");
	System.out.println();
	System.out.println();


}

public static Card CreateCard(String name, String phone, int CardID, int BookID)
{


}

public class Card {



}

public class Book {



}



View Postpbl, on 26 April 2011 - 08:46 PM, said:

If he delimiters are always the same, in your case "," a lot easier to use the split() method rather than a Tokenizer:

String line = fileinput.nextLine();
String[] token = line.split(",");

token[0] first string of the line
token[1] second String (after the ,)
....

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error (tokenizing) trying to read a txt file...

Posted 26 April 2011 - 09:16 PM

Just a few: This

// read the next line from the file
String[] tokens = line.split(",");
String line = fileinput.nextLine();

redefines line that was declared a few lines before

// read the next line from the file
String line = fileinput.nextLine();
try {

books[i] = new Book(tokens[0], tokens[1], parseInt(tokens[2]), false, 0);
should be
books[i] = new Book(tokens[0], tokens[1], Integer.parseInt(tokens[2]), false, 0);

while(command != 0) // command is not defined/declared
Was This Post Helpful? 0
  • +
  • -

#5 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Error (tokenizing) trying to read a txt file...

Posted 26 April 2011 - 09:50 PM

Thanks, it worked partially. I changed the lines accordingly and fixed the while loop error...
Both of the new errors are "cannot find symbol" @line 44, 49


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Library {
	public static void main(String[] args, int i) {
	
	// declare an array of 20 cards
	Card[] cards = new Card[20];
	// declare an array of 20 books
	Book[] books = new Book[20];

	// declare a file pointer and open the cards file 
	Scanner fileinput;
        try {
            fileinput = new Scanner(new File("cards.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Program execution ends because the input file cannot be found");
           return;

        }
	
	// read the next line from the file
	String line = fileinput.nextLine();
        try {
            // declare a file pointer and open the books file
            fileinput = new Scanner(new File("books.txt"));
        } catch (FileNotFoundException x) {
           System.out.println("Program execution ends because the input file cannot be found");
           return;

        }

        while(fileinput.hasNext())
        {
            String title =  fileinput.next();
            String author = fileinput.next();
            int bookid = fileinput.nextInt();

            }
	// read the next line from the file
	line = fileinput.nextLine();
        String[] tokens = line.split(",");
        books[i] = new Book(tokens[0], tokens[1], Integer.parseInt(tokens[2]), false, 0);
            i++;
	// tokenize the line to get the data for class variables
       
	// display main menu
	ShowMenu();
        
        int command;
        Scanner user_input = new Scanner(System.in);
        command = user_input.nextInt();
	while(command != 0)
	{
		ShowMenu();
	}


}
}

public static void ShowMenu()
{

        System.out.println();
	System.out.println("     ******  Menu   ********  ");
	System.out.println("  1 - Show all library Books ");
	System.out.println("  2 - Check out a book ");
	System.out.println("  3 - Check in a book ");
	System.out.println("  4 - display this menu ");
	System.out.println();
	System.out.println();


}






View Postpbl, on 26 April 2011 - 09:16 PM, said:

Just a few: This

// read the next line from the file
String[] tokens = line.split(",");
String line = fileinput.nextLine();

redefines line that was declared a few lines before

// read the next line from the file
String line = fileinput.nextLine();
try {

books[i] = new Book(tokens[0], tokens[1], parseInt(tokens[2]), false, 0);
should be
books[i] = new Book(tokens[0], tokens[1], Integer.parseInt(tokens[2]), false, 0);

while(command != 0) // command is not defined/declared

This post has been edited by xterminal01: 26 April 2011 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error (tokenizing) trying to read a txt file...

Posted 27 April 2011 - 09:15 PM

You have an extra } here

	while(command != 0)
	{
		ShowMenu();
	}


}
}  <-----------------



as far as the error at line 44 is concerned are you sure your Book class a constructor that match the signature of your parameters ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1