Java ArrayList学生登录编译错误
我的代码有问题。我正在尝试创建一个学生日志,如果输入正确,将允许用户访问memberEnter()部分,如果输入错误,系统将退出 我一直在尝试编译代码,但它给了我一个错误。有人能帮我修复这个问题吗 我得到的错误是: cannot find symbol - variable studentidlogin 在Java ArrayList学生登录编译错误,java,search,arraylist,system,bluej,Java,Search,Arraylist,System,Bluej,我的代码有问题。我正在尝试创建一个学生日志,如果输入正确,将允许用户访问memberEnter()部分,如果输入错误,系统将退出 我一直在尝试编译代码,但它给了我一个错误。有人能帮我修复这个问题吗 我得到的错误是: cannot find symbol - variable studentidlogin 在MainSystem的main方法中(也在代码本身中标记) 主要系统代码: import java.io.*;//imports the io package import java.util
MainSystem
的main
方法中(也在代码本身中标记)
主要系统代码:
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.
}
}
}
}
成员类别:
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
}
}
import java.util.*;
导入java.io.*;
/**
*在这里写一个类成员的描述。
*
*@author(你的名字)
*@version(版本号或日期)
*/
公共班级成员
{
私有列表memberList;//创建数组列表
公职人员()
{
memberList=new ArrayList();//使“memberList”成为数组列表。
}
公共无效新成员(学生注册学生)
{
memberList.add(student);//将学生添加到数组列表中。
}
公共字符串toString()
{
字符串totalmem=“\n”;
对于(int i=0;i
对于,您在if语句中执行了mem.studentidlogin而不是studentidlogin:
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));
if(studentidlogin.equals("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
你的职能
public String studentidlogin(String studentidlogin)
接受一个字符串参数,但您是这样调用它的:
mem.studentidlogin = ("\n Not a member ")
编辑:
只是为了扩展我的答案。你还有一些问题要解决。也许阅读java基础知识会有所帮助
e、 g
要比较两个字符串并使用赋值运算符(=),
但在你的情况下,你需要equals方法
String str = "anything";
if (str.equals("anything")) {
//do something
}
我想您的意思是使用==运算符,但这只比较了两个内存点。您的代码:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
println()
调用中的mem.studentidlogin
的第一次使用是正确的。由于studentidlogin
是成员类的方法,因此应将其作为方法调用,并带有与其参数匹配的括号和参数
第二种用法是不正确的。您将其视为一个字段。我想您可能期望这是从上一次调用返回的结果,但如果是这样,Java就不能这样工作
此外,我相信您正在尝试将其与字符串“\n非成员”
进行比较。只能使用equals()
或equalsIgnoreCase()
对字符串进行比较
从代码的角度看,您似乎在尝试为一个字段(不存在)赋值,该字段位于只需要布尔值的if
语句中(比较将是=
,正如我所说,您无论如何都不应该使用它,但您使用的是赋值=
)
您有两种选择。请正确地再次调用:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin().equals("\n Not a member "))
或者,为了避免所有工作重复两次,最好定义一个变量:
String loginResult = mem.studentidlogin(studentidlogin);
System.out.println(loginResult);
if( loginResult.equals("\n Not a member "))
错误是什么?OO找不到symbol-变量studentidlogin表示错误,但永远不要使用“+”表示字符串。请使用StringBuilder。如果(mem.studentidlogin=(“\n非成员”))在更改该错误时出现另一个错误,则在主系统部件中找不到symbol-变量studentidlogin代码行:“不兼容的类型:java.lang.string无法转换为布尔值”
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin().equals("\n Not a member "))
String loginResult = mem.studentidlogin(studentidlogin);
System.out.println(loginResult);
if( loginResult.equals("\n Not a member "))