logo
down
shadow

I have created a small book program, I am new to java. Can't find what the error or mistake in this code?


I have created a small book program, I am new to java. Can't find what the error or mistake in this code?

By : Sai Ram Rallabandi
Date : November 22 2020, 02:59 PM
With these it helps You had bracket problems. Also if you want to run your main you will have to move the main outside of the BookMgrSys class. The following code will allow you to run the application.
code :
import javax.swing.*;
import java.util.*;
import java.io.*;

class BookInfo
{
    String ISBN;
    String BookName;
    String AuthorName;
    String TotalBooks;

    // parameterized constructor
    // public (){}

    public BookInfo(String i, String b, String a)
    {
        ISBN = i;
        BookName = b;
        AuthorName = a;
    }

    ArrayList<BookInfo> books;

    // constructor
    public BookInfo()
    {

        books = new ArrayList<BookInfo>();
        loadbooks();
    }

    public void loadbooks()
    {
        String tokens[] = null;
        String no, name, author;
        try
        {
            FileReader fr = new FileReader("books.txt");
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while(line != null)
            {
                tokens = line.split(",");
                no = tokens[0];
                name = tokens[1];
                author = tokens[2];
                BookInfo b = new BookInfo(no, name, author);
                books.add(b);
                line = br.readLine();
            }
            br.close();
            fr.close();
        }
        catch(IOException ioEx)
        {
            System.out.println(ioEx);
        }
    }

    public void savebooks()
    {
        try
        {
            BookInfo b;

            String line;
            FileWriter fw = new FileWriter("books.txt");
            PrintWriter pw = new PrintWriter(fw);
            for(int i = 0; i < books.size(); i++)
            {
                b = (BookInfo) books.get(i);
                line = b.ISBN + "," + b.BookName + "," + b.AuthorName;
                // writes line to file (books.txt)
                pw.println(line);
            }
            pw.flush();
            pw.close();
            fw.close();
        }
        catch(IOException ioEx)
        {
            System.out.println(ioEx);
        }
    }

    // add new book record to arraylist after taking input
    public void addbook()
    {
        String ISBN = JOptionPane.showInputDialog("Enter ISBN");
        String BookName = JOptionPane.showInputDialog("Enter Book Name");
        String AuthorName = JOptionPane.showInputDialog("Enter Author Name");
        // construct new book object
        BookInfo p = new BookInfo(ISBN, BookName, AuthorName);
        // add the above BookInfo object to arraylist
        books.add(p);
    }

    // search book record by name by iterating over arraylist
    public void searchBook(String n)
    {
        for(int i = 0; i < books.size(); i++)
        {
            BookInfo p = (BookInfo) books.get(i);
            if(n.equals(p.AuthorName))
            {
                p.print();
            }
        }
    }

    // delete book record by name by iterating over arraylist

    public void deleteBook(String n)
    {
        for(int i = 0; i < books.size(); i++)
        {
            BookInfo p = (BookInfo) books.get(i);
            if(n.equals(p.BookName))
            {
                // p.remove();
            }
        }
    }

    public void print()
    {
        // print your book here
    }

    class BookMgtSys
    {
    }

    public static void main(String args[])
    {
        BookInfo p = new BookInfo();
        String input, s;
        int ch;
        while(true)
        {
            input = JOptionPane.showInputDialog(
                    "Enter 1 to add " + "\n Enter 2 to             Search \n Enter 3 to Delete" + "\n Enter 4 to Exit");
            ch = Integer.parseInt(input);
            switch(ch)
            {
            case 1:
                p.addbook();
                break;
            case 2:
                s = JOptionPane.showInputDialog("Enter name to search ");
                p.searchBook(s);
                break;
            case 3:
                s = JOptionPane.showInputDialog("Enter name to delete ");
                p.deleteBook(s);
                break;
            }
        } // end while
    }// end main
} // end class


Share : facebook icon twitter icon
Mistake in this small Java program

Mistake in this small Java program


By : Bridget Ludwa O'Hanl
Date : March 29 2020, 07:55 AM
this will help What is the mistake in this code? When I ran the program, it says "The application buttonProj (process com.example.buttonproj) has stopped unexpectedly. Please try again" I just tried to create an Add and Subtract button and user can click on it to increase or decrease number i. , use
code :
display.setText(""+counter);
 //OR
display.setText(String.valueOf(counter));
display.setText(counter);
I need to find the book code and book title for each book found in branch number 2 and written by author 20

I need to find the book code and book title for each book found in branch number 2 and written by author 20


By : user2817982
Date : March 29 2020, 07:55 AM
this one helps. want to find the book code and book title for each book found in branch number 2 and written by author 20. , You can use simple join between the tables.
code :
SELECT B.BOOK_CODE, B.TITLE
FROM BOOK B
JOIN WROTE W
ON W.BOOK_CODE = B.BOOK_CODE
AND W.AUTHOR_NUM ='20'
JOIN INVENTORY I
on I.BOOK_CODE = B.BOOK_CODE
AND I.BRANCH_NUM ='2'
issue with java code . i cant find my mistake. Dog and Cat are undelined

issue with java code . i cant find my mistake. Dog and Cat are undelined


By : Philipp Dusch
Date : March 29 2020, 07:55 AM
To fix this issue The problem is that the compiler doesn't know where the Dog or Cat class is. Here are the steps to telling the compiler how to create a new Dog or new Cat
Make sure that you have a Dog.java and a Cat.java file in your project. Even if they are in the same folder as your PetsDriver, the compiler won't know to compile them unless you add them to your netbeans project.
Where did i make a mistake in my program i think its a logical error but i cant quite find it

Where did i make a mistake in my program i think its a logical error but i cant quite find it


By : ivanputeraa
Date : March 29 2020, 07:55 AM
seems to work fine I have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here? , The biggest error is
code :
while(loop_counter != 21);
simple java program find mistake

simple java program find mistake


By : David
Date : March 29 2020, 07:55 AM
around this issue System.out.println() calls toString() method, which comes from basic Object class. There are two ways of fixing this code:
1) Override the standart toString() method:
code :
class WriteOut {
    private static int sumas;
    @Override
    public String toString(){
        return String.valueOf(sumas); // returns a string with sumas value
    }

    public static void main(String arg[]) {
        WriteOut sum =new WriteOut();
        sum.sud(5);
        System.out.println("suma: "+sum);
    }
}
class WriteOut {
    public static int sumas; // To allow System.out.println() to see this variable
    public void sud(int sds) {  
        sumas = sds;
    }

    public static void main(String arg[]) {
        WriteOut sum =new WriteOut();
        sum.sud(5);
        System.out.println("suma: "+sum.sumas);
    }
}
Related Posts Related Posts :
  • Failed to find provider info for urbanairship.provider
  • Android ListView Adapter Crash issue/Duplicates
  • Displaying Splash screen only once on user preference
  • Loop List<WebElement> doesn't stop despite no more WebElements
  • Check if pattern exists in a String
  • Swing - Key Problems (Arrow Keys, Tab, etc.) in GUIs with TextComponents
  • Why the solution for binary search tree validity is not working?
  • Double encrypting 2048 RSA?
  • create junit test for runnable class
  • Unable to read and sort information from a text file in Java
  • My device isn't getting detected in android studio (driver issue)
  • Representing a Graph in Java
  • How to XML mapping using jxls
  • Quick Sort stackoverflow error for large arrays
  • Parsing Apache Tika XML Output returns Unknown Tag
  • java simple UI loop, can't find bug
  • how to use greater than and less than in a single if statement in java
  • Error:(27, 13) Failed to resolve: com.antonyt.infiniteviewpager:library:1.0.0
  • Gradle dependencies on source set of different projects
  • Session Scope in Restful Spring Services
  • Java: Change a value in an overridden method of a different class to a value that was calculated in the class
  • Event notification if SWT Combo display list is visible
  • Hibernate WrongClassException for Custom Discriminators
  • User define enum element method in Java? /Hearthstone Text Game/
  • Java Multi-dimensional Array Average
  • Doubly Linked List delete last
  • First time Fragment, and I got "Application has stopped" when I try open it with fragmentTransaction
  • How to model two one-to-one relationships between the same two tables in Hibernate?
  • IntelliJ - include pom.xml dependencies in lib directory
  • Tomcat 7 : Filtering lifecycle events
  • JSONException: Names must be strings
  • Build Spark JavaRDD List from DropResult objects
  • Include css file in richlet with zk
  • Jackson CSV's WRAP_AS_ARRAY
  • Priority for a web request in Java web applications
  • Is there a better way to format this timestamp to ISO8601?
  • Issue with Spring Scope
  • How to declare and use an array of dictionaries from Python in Java and filter them?
  • Wrong weekdays from Calendar.getDisplayName()
  • ClassNotFoundException when creating new ClassPathXmlApplicationContext
  • Java Method Call from Thread
  • Remove background noise from image to make text more clear for OCR
  • PostgreSQL query always return true
  • How to connect FX controller with main app
  • Is there a difference between these two code snippets, and if so what?
  • When two interfaces have conflicting return types, why does one method become default?
  • Updating database throws errors in To Do List app
  • Where does the url come from?
  • Dagger 2 issue overriding single provides annotated method from a module in a library which app uses
  • Better way then If(x instanceof y) in Java?
  • how to debug maven enunciate plugin tomcat web service
  • Spring Context Initialization fails when upgrading to version 4.2.3
  • spring boot cannot connect to rabbitmq
  • How to Run Unit Test in Parallel Across All Modules Using Maven?
  • Getting the next Value in a ArrayList(Not working)
  • How to wait for a MouseListener mouse press?
  • Java RMI: Blocking for void method return?
  • Get Table Properties out of Hive using Java API
  • Read CSV file and write to another CSV - ArrayIndexOutOfBoundsException and pattern difficuties
  • Set LineChart(javafx) style to a child element
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co