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.name) ) {
p.remove();
}
}
}
} // end class
}
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;
System.exit(0);
}
}//end while
}//end main
}
This program just take book name,edit and delete option. But when I compile it gives me errors as shown below:
Process started >>>
BookMgtSys.java:92: error: illegal start of expression
public void deleteBook (String n) {
^
BookMgtSys.java:92: error: illegal start of expression
public void deleteBook (String n) {
^
BookMgtSys.java:92: error: ';' expected
public void deleteBook (String n) {
^
BookMgtSys.java:92: error: ';' expected
public void deleteBook (String n) {
^
4 errors
<<< Process finished. (Exit code 1)
searchBook
method. Add a }
right before the comment //delete book ...
// end class
BookMgtSys
class BookInfo
doesn't have methods remove
or print
. Quite naturally the compiler complains about it. Also there is no field name
Most importantly make sure you format and indent your code. Such syntax errors will become more obvious if the source code is formatted and indented
Seriously, use some IDE . Notepad++ is not what you want. Herebelow is pretty-printed and fixed version of your code, however you still need to work through it.
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
}
} // end class
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
}
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.