I'm having a problem with my code. I am trying to create a student log in which when entered correctly will allow the user to access the memberEnter() part and if its wrong the system should exit.
I've been trying to compile the code and it's been giving me an error can anyone help me fix this?
The error I get is:
cannot find symbol - variable studentidlogin
The line where the error occurs is
if(mem.studentidlogin = ("\n Not a member "))
In the main
method of MainSystem
(also marked in the code itself).
Main System Code:
import java.io.*;//imports the io package
import java.util.Scanner;//imports scanner
import java.util.*;//imports the util package
public class MainSystem {
static String fileName = null;
static Library lib = new Library();
static Scanner in = new Scanner(System.in);
static Boolean running = true;
static Member mem = new Member();//static variables that can be initialised at compile time, but can be modified at run time
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
/************* ERROR OCCURS HERE **************/
if(mem.studentidlogin = ("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
private static void newMember() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int libraryNumber;
String StudentID, Username, FullName, Address, email, PhoneNumber;
System.out.println("\nEnter Library Number: ");
libraryNumber= Integer.parseInt(br.readLine());;//prompts user to enter library number.
System.out.println("\nEnter Student ID: ");//prompts user to enter student id.
StudentID = br.readLine();
System.out.println("\nEnter Full Name: ");//prompts user to enter full name
FullName = br.readLine();
System.out.println("\nEnter Username: ");//prompts user to enter username
Username = br.readLine();
System.out.println("\nEnter E-Mail Address: ");//prompts user to enter an email address
email = br.readLine();
System.out.println("\nEnter Home Address: ");//prompts user to enter home address
Address = br.readLine();
System.out.println("\nEnter Phone Number: ");//prompts user to enter phone number
PhoneNumber = br.readLine();
StudentSignUp b = new StudentSignUp(libraryNumber, FullName, StudentID, Username, Address, email, PhoneNumber);
mem.newMember(b);
System.out.println("\nThankyou. You can now Login.: ");// prompts user that they can now sign in.
}
private static void memberEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for Search For A Book");//options available if member is enters.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved script
break;
case 1:
saveAndQuit();//saves and quits from the system
break;
case 2:
System.out.println(lib.toString());//lists all the books in the library
break;
case 3:
searchBook();//searches for a book
break;
}
}
System.exit(0);
}
private static void librairanEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for add book to library"
+ "\nEnter 4 for Search For A Book"
+ "\nEnter 5 for list of current members"
+ "\nEnter 6 to borrow a book"
+ "\nEnter 7 to return a book");//options available if the librarian logs in.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved libraries
break;
case 1:
saveAndQuit();//saves and quits
break;
case 2:
System.out.println(lib.toString());//prints current books in library
break;
case 3:
addBook();//adds a book to the system
break;
case 4:
searchBook();//searches for a book in the system
case 5:
System.out.println(mem.toString());//list of current members
break;
case 6:
borrowBook();//allows to borrow a book.
case 7:
returnBook();//allows to return a book.
}
}
System.exit(0);//exits from the system/
}
private static void addBook() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int isbn, numcopies;
String author, title, genre;// initializes the variables
System.out.println("\nEnter ISBN: ");
isbn = Integer.parseInt(br.readLine());//prompts user to enter isbn
System.out.println("\nEnter Author: ");//prompts user to enter the auth
author = br.readLine();
System.out.println("\nEnter Title: ");//prompts user to enter title
title = br.readLine();
System.out.println("\nEnter Genre: ");//prompts user to enter genre
genre = br.readLine();
System.out.println("\nEnter Number Of Copies: ");//prompts user to enter number of copies
numcopies = Integer.parseInt(br.readLine());
Book b = new Book(isbn, author, title, genre, numcopies);//Creates the new book and adds it to the library
lib.addBook(b);
}
private static void saveAndQuit() {//saves and quits
// TODO Auto-generated method stub
System.out.println("Enter file name: ");//prompts the user to enter the file name.
fileName = in.next() + ".ser";//saves the file name
running = false;
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(lib);
fos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void loadScript(String name) {//loads a previously saved script
// TODO Auto-generated method stub
FileInputStream fis = null;
ObjectInputStream in = null;
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
lib = (Library) in.readObject();
fis.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("\nThe file does not exist!");
}
}
private static void searchBook() throws IOException//allows searching for a book
{
String titleSearch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."////prompts the user to answer 0
+ "\nEnter 3 to go back");//promts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the title of the book
titleSearch = br.readLine();
System.out.println(lib.searchTitle( titleSearch));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void borrowBook() throws IOException
{
String titleBorrow;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."//prompts the user to answer 0
+ "\nEnter 3 to go back");//prompt the user to answer 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user ot enter the book title
titleBorrow = br.readLine();
System.out.println(lib.borrowBook( titleBorrow));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void returnBook() throws IOException
{
String returnedBook;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to retrun a book."//promts the user to enter 0
+ "\nEnter 3 to go back");//prompts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the book title.
returnedBook = br.readLine();
System.out.println(lib.returnBook( returnedBook));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
}
Member Class:
import java.util.*;
import java.io.*;
/**
* Write a description of class Member here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Member
{
private List<StudentSignUp> memberList;//creates the array list
public Member()
{
memberList = new ArrayList<StudentSignUp>(); //makes the 'memberList' the array list.
}
public void newMember(StudentSignUp student)
{
memberList.add(student);//adds a student to the array list.
}
public String toString()
{
String totalmem = "\n ";
for (int i=0; i<memberList.size(); i++)
{
StudentSignUp b = memberList.get(i);
totalmem = totalmem + b.toString(); //prints out all students
}
return totalmem;
}
public String studentidlogin(String studentidlogin) {
if (studentidlogin == null) return "\n Not a member ";
for(int i = 0; i < memberList.size(); i++){
if(studentidlogin.equalsIgnoreCase(memberList.get(i).getStudentID())){
return memberList.get(i).toString();//allows user to search for a book in the system
}
}
return "\n Not a member "; //reachable only if no book found
}
}
Your code:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
The first usage of mem.studentidlogin
, in the println()
call, is correct. Since studentidlogin
is a method of the Member
class, it should be called as a method - with parentheses and arguments that match its parameters.
The second usage is incorrect. You are treating it as if it was a field. I think perhaps you expected this to be the result that was returned from the previous call, but if so, Java doesn't work that way.
Besides, I believe you are trying to compare it to the string "\n Not a member "
. Comparison of strings should only be done with equals()
or equalsIgnoreCase()
!
As the code stands, it looks like you are trying to assign a value to a field (that doesn't exist), inside an if
statement that only expects a boolean value (comparison would be ==
which, as I said, you shouldn't use anyway, but you are using assignment =
).
You have two options. Either call it again properly:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin().equals("\n Not a member "))
Or preferably, to avoid doing all the work twice, define a variable:
String loginResult = mem.studentidlogin(studentidlogin);
System.out.println(loginResult);
if( loginResult.equals("\n Not a member "))