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 4 of 4

Thread: My text input file is not working correctly?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My text input file is not working correctly?

    ublic class Book
    {
    String author, area, isbn;
    int length;

    public Book(String isbn, String author, String area, int length)
    {
    this.isbn = isbn;
    this.author = author;
    this.area = area;
    this.length = length;
    }

    public boolean isLong()
    {
    if(length > 600)
    return true;
    else
    return false;
    }

    public String getAuthor()
    {
    return author;
    }

    public String getArea()
    {
    return area;
    }

    public int getLength()
    {
    return length;
    }

    public String toString()
    {
    String s;
    return s = "[BOOK ISBN: " +isbn + ", AUTHOR: " + author + "AREA: " + area + "PAGES: " + length + "]";
    }

    public String mathCS()
    {
    if (area.equals("MATH"))
    return "MATH";
    if (area.equals("CS"))
    return "CS";
    else
    return "Other";
    }

    }



    import java.util.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.text.DecimalFormat;
    public class BookCollection
    {
    private ArrayList<Book> bookList = new ArrayList<Book>();

    public void BookCollecion() throws IOException
    {
    Scanner scan;
    Scanner fileScan = new Scanner(new File("Books.txt"));
    String aLine, isbn, author, area;
    int length;
    Book aBook;

    while(fileScan.hasNext())
    {
    aLine = fileScan.nextLine();
    scan = new Scanner(aLine);

    isbn = scan.next();
    author = scan.next();
    area = scan.next();
    length = scan.nextInt();

    aBook = new Book(isbn, author, area, length);
    bookList.add(aBook);
    }

    }

    public void displayLongBooks()
    {
    System.out.println("LONG BOOKS");
    for(int i = 0; i<bookList.size(); i++)
    {
    if(bookList.get(i).isLong())
    {
    bookList.get(i).toString();
    }
    }

    }


    public void displayAverageLength()
    {
    double avg = 0;

    for(int i = 0; i < bookList.size(); i++)
    {
    avg += bookList.get(i).getLength();
    }
    System.out.println("AVERAGE LENGTH\n");
    System.out.println(avg/bookList.size());
    }

    public void displayMathCSBooks()
    {
    System.out.println("");
    System.out.println("MATH and COMPUTER SCIENCE BOOKS");

    for(int i = 0; i < bookList.size(); i++)
    {
    if(bookList.get(i).mathCS().equals("MATH") || bookList.get(i).mathCS().equals("CS"))
    bookList.get(i).toString();
    }

    }

    public void displayBooksFromAuthor(String author){
    int a = 0;
    System.out.println(author + "'s BOOKS");

    for(int i = 0; i < bookList.size(); i++)
    {
    if (bookList.get(i).getAuthor().equals(author)){
    System.out.println (bookList.get(i));
    a++;}
    }
    if (a == 0)
    System.out.println("There are no books authored by " + author + ".");

    }

    public void displayBooksFromArea (String area)
    //displays all books from given area (in analogous way)
    {

    for(int i = 0; i < bookList.size(); i++)
    {
    if (bookList.get(i).getArea().equals(area))
    bookList.get(i);
    }
    }
    public void displayOtherBooks()
    {
    System.out.println("");
    System.out.println("OTHER BOOKS");

    for(int i = 0; i < bookList.size(); i++)
    {
    if(bookList.get(i).mathCS().equals("Other"))
    bookList.get(i).toString();
    }

    }

    public String toString()
    {
    String book = "ALL BOOKS\n";

    for(int i = 0; i<bookList.size(); i++)
    book += bookList.get(i) + "\n";

    return book;
    }
    }

    import java.io.*;
    import java.util.Scanner;
    public class TestBookCollection {

    public static void main(String args[]) throws IOException
    {
    Scanner input = new Scanner(System.in);
    BookCollection test = new BookCollection();

    test.toString();
    test.displayLongBooks();

    System.out.println();
    System.out.print("Enter author name: ");
    String author = input.nextLine();
    System.out.println();
    test.displayBooksFromAuthor(author);

    System.out.println();
    System.out.print("Enter another author name: ");
    author = input.nextLine();
    System.out.println();
    test.displayBooksFromAuthor(author);


    //test.displayBooksFromArea(area);

    test.displayAverageLength();

    test.displayMathCSBooks();

    test.displayOtherBooks();
    }
    }


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My text input file is not working correctly?

    Content Within Text File:

    1001-01-171714 Lewis CS 699
    1002-01-771424 Lewis CS 477
    1003-01-141434 Jones MATH 698
    1004-01-141444 Jones CS 617
    1005-01-141454 Brown CHEM 326
    1006-01-141464 Jones BIO 127
    1007-01-771474 Sanders CS 998

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My text input file is not working correctly?

    I see that you've posted a bit of code and the contents of a file, but I can't find a clear well defined question anywhere. What exactly are you asking here?

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

    Default Re: My text input file is not working correctly?

    Also please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. My code is not working correctly
    By shavek911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2012, 05:49 PM
  2. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  3. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM
  4. Replies: 1
    Last Post: July 16th, 2011, 08:55 AM
  5. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM