I'm having a problem with an arrayList in which I store member objects. I think it might be something to do with the way i've declared it. This is what I have so far
Member Class
package assignment;
import java.util.ArrayList;
public class Member {
private int memberID;
private String memberName;
private int memberAge;
private int numOfBooksLoaned;
private int penalties;
public Member(int memberID, String memberName, int memberAge, int numOfBooksLoaned, int penalties) {
this.memberID = memberID;
this.memberName = memberName;
this.memberAge = memberAge;
this.numOfBooksLoaned = numOfBooksLoaned;
this.penalties = penalties;
}
public int getMemberID() {
return memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public int getMemberAge() {
return memberAge;
}
public void setMemberAge(int memberAge) {
this.memberAge = memberAge;
}
public int getNumOfBooksLoaned() {
return numOfBooksLoaned;
}
public void setNumOfBooksLoaned(int numOfBooksLoaned) {
this.numOfBooksLoaned = numOfBooksLoaned;
}
public int getPenalties() {
return penalties;
}
public void setPenalties(int penalties) {
this.penalties = penalties;
}
}
MemberList class
package assignment;
import java.util.Scanner;
import java.util.ArrayList;
public class MemberList {
private ArrayList<Member> Members;
public MemberList(ArrayList<Member> Members)
{
this.Members = Members;
}
public void addNewMember()
{
Scanner input = new Scanner(System.in);
boolean successful = false;
int memberID;
String memberName;
int memberAge;
do {
System.out.println("/t/tCreate new member" + "/nPlease enter your full name: ");
memberName = input.nextLine();
for (int i = 0; i < Members.size(); i++) {
if (Members.get(i).getMemberName().equalsIgnoreCase(memberName)) {
System.out.println("This member name is already in use");
} else {
successful = true;
}
}
} while (successful == false);
The problem is just after the for loop in the if statement
if (Members.get(i).getMemberName().equalsIgnoreCase(memberName))
the error says that getMemberName() is undefined for type Member.
Any ideas?
I have used this exact same identical way of using an arrayList and it works fine but it isn't working now for some reason.
try to use a foreach loop in this way
for (Member m : Members) {
if (m.getMemberName().equalsIgnoreCase(memberName)) {
System.out.println("This member name is already in use");
} else {
successful = true;
}
}
Another thing, is discouraged to use Capital letter at first letter in class members in Java, define the list as
private ArrayList<Member> members;
Related
Hi so I have a project for college where I need to build a chat in java that to work only in on computer where you execute the code and you give the name of the two users and a translation value but I am having a problem with the way they want me to do it since I cant use super, arrays , extends, or inherences I am having a problem how it currently is I my Main like this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Chat ch = null;
boot(in, ch);
menu(in, ch);
}
public static Chat boot (Scanner in, Chat ch){
String name1;
String name2;
int transvalue = 0;
System.out.print("Username 1:");
name1=in.nextLine();
do{
System.out.print("Username 2:");
name2=in.nextLine();
if (name1.equals(name2)){
System.out.println("Names can't be equal");
}
}while(name1.equals(name2));
do{
System.out.print("Translation Value:");
transvalue=in.nextInt();
in.nextLine();
if (transvalue<1 || transvalue>26){
System.out.println("Value Can only be between 0 and 26");
}
}while(transvalue<1 || transvalue>26);
return new Chat(name1,name2,transvalue);
}
So this show be adding the info I put in to my class Chat
public class Chat {
private static final int id1 = 1;
private static final int id2 = 2;
private Users u1;
private Users u2;
private Conversation conv;
public Chat(String name1, String name2, int transvalue){
u1 = new Users(id1,name1);
u2 = new Users(id2,name2);
conv = new Conversation(transvalue);
}
public static int getId1() {
return id1;
}
public static int getId2() {
return id2;
}
public Users getU1() {
return u1;
}
public void setU1(Users u1) {
this.u1 = u1;
}
public Users getU2() {
return u2;
}
public void setU2(Users u2) {
this.u2 = u2;
}
public Conversation getConv() {
return conv;
}
public void setConv(Conversation conv) {
this.conv = conv;
}
}
but when I add the info and try to return it like for example:
System.out.print(ch.getU1());
or
System.out.print(ch.getU1().getName());
I am getting a null am I doing something wrong?
public class Users {
private int id ;
private String name;
private String mess;
public Users(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Can anyone find or point out what I'm doing wrong
Thank you in Advance to all that take the time to Help
I'm programming in a class and I need to have a variable from a different class. How can I do this?
package domein;
public class Speler
{
private String naam;
private String kleur;
private Sector sector;
private int Sector;
private int krediet = 10;
private int extraSchattingWaarde = 0;
private int nummer;
public Speler(String naam, String kleur, Sector sector)
{
setNaam(naam);
setKleur(kleur);
setSector(sector);
}
public String getNaam()
{
return this.naam;
}
public void setNaam(String naam)
{
//controle of het leeg is??
this.naam = naam;
}
public Sector getSector()
{
return this.sector;
}
private void setSector(Sector sector)
{
//tussen 1 en 4
this.sector = sector;
}
public String getKleur()
{
return this.kleur;
}
private void setKleur(String kleur)
{
//controle of het de beschikbare kleuren zijn
this.kleur = kleur;
}
public int getKrediet()
{
return this.krediet;
}
public void setKrediet(int krediet)
{
this.krediet = krediet;
}
public int getExtraSchattingWaarde()
{
return this.extraSchattingWaarde;
}
public void setExtraSchattingWaarde(int waarde)
{
this.extraSchattingWaarde = waarde;
}
}
This is the class where I need to get the variables and some methods. How can I make this class global?
Just a thing : this line is wrong private int Sector; because you can not use a class name as your variable name. This should hide your class visibility.
I assume this is an error and I continue the explanation.
In an other class you can instanciate this class and call the values. For example:
public class MyClass {
private Speler mySpeler = new Speler("AAA", "BBB", 3);
public MyClass() {}
public void myMethod() {
System.out.println(mySpeler.getKleur());
}
}
I have seen other array codes but here is what I got. I am trying to set the courseID in the CollegeCollege course in the array. I need to set the array in the student.java
public class CollegeCourse {
private static String courseID;
private static int creditHours;
private static char grade;
public static void setCourseID(String course){
courseID = course;
}
public static String getCourseID(){
return courseID;
}
public static void setCreditHours(int CH){
creditHours = CH;
}
public static int getCreditHours(){
return creditHours;
}
public static void setGrade(char g){
grade = g;
}
public static char getGrade(){
return grade;
}
public class Student {
private static int IDnumber;
private static CollegeCourse[] course = new CollegeCourse[5];
public static void setIDnumber(int x){
IDnumber = x;
}
public static int getIDnumber(){
return IDnumber;
}
public static String getCourse(int x){
return course[x].getCourseID();
}
public static void setCourse(CollegeCourse newCourse, int ID){
CollegeCourse.setCourseID = newCourse[ID];
}
}
It could seem like you are trying to code that the student takes course with id 5. Maybe your classes should have constructors taking arguments based on what ID the course has.
and I don't know what you need the fields to be static for, bt feel free to have them static if it is needed for some reason.
public class CollegeCourse {
private String courseID;
private int creditHours;
private char grade;
public CollegeCourse(String courseID) {
this.courseID = courseID;
}
...
public class Student {
...
private static CollegeCourse[] course = new CollegeCourse("5");
...
I think a better design would be this:
public class CollegeCourse {
private final String courseId;
private final int creditHours;
public CollegeCourse(final String courseId, int creditHours) {
this.courseId = courseId;
this.creditHours = creditHours;
}
public String getCourseId() {
return courseId;
}
public int getCreditHours() {
return creditHours;
}
#Override
public boolean equals(Object o) {
if (o == this) { return true; }
if (!(o instanceof CollegeCourse)) { return false; }
CollegeCourse cc = (CollegeCourse)o;
return courseId.equals(cc.courseId)
&& creditHours == cc.creditHours;
}
#Override
public int hashCode() {
int result = 17;
result = 31 * result + courseId.hashCode();
result = 31 * result + creditHours;
return result;
}
};
public enum Grade { A, B, C, D, E };
public class Student {
private final String id;
private final Map<CollegeCourse, Grade> course2GradeMap = new HashMap<>();
public Student(final String id) {
this.id = id;
}
public Grade getGrade(final CollegeCourse course) {
return couse2GradeMap.get(course);
}
public void addCollegeCourse(final CollegeCourse course, Grade grade) {
course2GradeMap.put(course, grade);
}
public Collection<CollegeCourse> getCollegeCourses() {
return Collections.unmodifiableCollection(course2GradeMap.values());
}
};
Use it in this way:
Student student = new Student("001");
student.addCollegeCourse(new CollegeCourse('alg', 100), Grade.A);
student.addCollegeCourse(new CollegeCourse('stat', 100), Grade.C);
An unexpected error occured caused by exception IllegalArgumentException:
Expecting collection type [[Lmodels.Question;]
This error appears in localhost when I run the whole project on the browser. I don't understand the error, I checked the all other classes linked to the class Question in a way or another but there isn't any errors, the whole code doesn't have any compilation errors.
This is the class of model Question:
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
#Entity
public class Question extends Model{
public int ExerciseID;
public String theQuest;
public int marks;
public String [] choices;
#ManyToOne
public Exercise myExercise;
#OneToOne (mappedBy="question")
public ModelAn modelAnswer;
public Question (Exercise e, String quest,String [] aofc,int mark){
if(myExercise.noOfQuest<50){
int j = 0;
if (quest != null){
for(int i = 0;i<myExercise.questions.length;i++){
String tempquestion = myExercise.questions[i].theQuest;
if (quest.equals(tempquestion)){
j++;
}}
if(j == 0){
myExercise = new Exercise(e.CreatorID,e.tutID,e.noOfQuest);
myExercise.setPeopleAnswered(e.getPeopleAnswered());
theQuest = quest;
while (aofc.length<=5){
this.choices[0] = aofc[0];
this.choices[1] = aofc[1];
this.choices[2] = aofc[2];
this.choices[3] = aofc[3];
this.choices[4] = aofc[4];
}
this.marks = mark;
}
}
else{
System.out.println("no question was entered please enter a question");
}}else{
System.out.println("the max no of questions was exceeded");
}
}
public void addChoices(Question theQuest,String choice1 , String choice2, String choice3, String choice4, String choice5){
String [] tempchoices = new String[5];
tempchoices[0] = choice1;
tempchoices[1] = choice2;
tempchoices[2] = choice3;
tempchoices[3] = choice4;
tempchoices[4] = choice5;
int j = 0;
for(int i = 1; i <= 5;i++){
if(tempchoices[i] != null){
j++;
}
}
if(j<2){
System.out.println("the options entered are less than 2");
}
else{
theQuest.choices[0] = choice1;
theQuest.choices[1] = choice2;
theQuest.choices[2] = choice3;
theQuest.choices[3] = choice4;
theQuest.choices[4] = choice5;
}
}
public ModelAn getModelAnswer() {
return modelAnswer;
}
public void setModelAnswer(ModelAn modelAnswer) {
this.modelAnswer = modelAnswer;
}
public String getTheQuest() {
return theQuest;
}
public void setTheQuest(String theQuest) {
this.theQuest = theQuest;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public String[] getChoices() {
return choices;
}
public void setChoices(String[] choices) {
this.choices = choices;
}
public Exercise getMyExercise() {
return myExercise;
}
public void setMyExercise(Exercise myExercise) {
this.myExercise = myExercise;
}
}
This error means that you annotated a field of type Question[] as #OneToMany or #ManyToMany somewhere.
You should use collection type such as List<Question> instead, because #OneToMany and #ManyToMany can only be placed on fields of collection types.
This is not supposed to be a client class. I'm just making a class for others to use. I'm using this for a Highschool. For example i have classes for the address, teacher, students, principal, roomnumber, etc..But its not compiling for some odd reason. I believe its because I'm not declaring a field but not sure.
import java.io.*;
public class HighSchool {
// Constructors
public HighSchool() { }
public HighSchool(String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects ) {
this.title = title;
this.teacher = teacher;
this.roomNumber = roomNumber;
this.period = period;
this.String[] students = students;
this.String address =a ddress;
this.String subjects = subjects;
}
public class Classcourse (String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects
private String period;) {
public String gettitle() {
return title;
}
public void settitle(String title) {
this.title = title;
}
public String getteacher() {
return teacher;
}
public void setteacher(String teacher) {
this.teacher = teacher;
}
public int getroomNumber() {
return roomNumber;
}
public void setroomNumber (int roomNumber) {
this.roomNumber = roomNumber;
}
public String getperiod() {
return getperiod();
}
public void setperiod (String period) {
this.period = period;
}
public String[] getstudents () {
return students[];
}
public void setstudents[] (String[] students
private String address;) {
this.students = students;
}
public String getaddress() {
return address;
}
public void setaddress (String address) {
this.address = address;
}
public String getsubjects() {
return subjects;
}
public void setsubjects (String subjects) {
this.subjects = subjects;
}
}
// modifier method
public void addstudents(String students) {
String[] newstudents = new String[students.length + 1];
for (int i = 0; i < students.length; i++) {
newstudents[i] = students[i];
}
newstudents[students.length] = student;
students = newstudents;
}
public boolean isInClass(String students) {
for (int i = 0; i < students.length; i++) {
if (students[i].equals(students)) {
return true;
}
}
return false;
}
// static creator method
public static HighSchool readFromInput() throws IOException {
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a HighSchool title: ");
HighSchool newHighSchool = new HighSchool(kb.readLine());
String students = null;
do {
System.out.print("Enter a student, or press Enter to finish: ");
students = kb.readLine();
if (students != null){
newHighSchool.addstudents(students);
}
} while (students != null);
return newHighSchool;
}
// Variables (Fields)
private String title;
private String[] students;
}
In addition, you wrote something that doesn't make sense from the point of view of Java Compiler:
private String period;) {
- probably remove ")".
The second thing:
Take a look on the declaration of class Classcourse.
It rather sounds wrong, although it can be an issue of this site's editor or something...
An "overall" hint - java has a very "intelligent" compiler in the most of the cases it can say what's wrong exactly with your code, so, assuming you're a newbie in Java, try to understand what compiler says to you.
Good luck!
Some things I noticed about the code:
public String getperiod() {
return getperiod();
}
This code will cause a endless loop when you call this function.
private String address;) {
this.students = students;
}
The compiler will give an error about the ";)". Change it to "()" to fix this.
Furthermore, you should really tell us more about the errors it's giving you. We can't help you if you don't give us the compiler errors.