Jump to content

How to compare the value of to Strings? Urgent Help!!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
Class 1:

import

java.util.*;

class

Teacher {

privateintamount;
private String tName;
privateinttestNum;


publicvoid getData(){
Scanner indata = new Scanner(System.in);


System.out.print("Enter the amount: ");
amount = indata.nextInt();


for(int num = 1; num <= amount; num++){
System.out.print("Enter the name of teacher " +(num)+": ");
tName = indata.nextLine();
indata.nextLine();
System.out.print("Enter the amount of test: ");
testNum = indata.nextInt();


}


}



//Return the teacher name
public String getName(){
returntName;
}



//Return the test Amount
publicint getTest(){
returntestNum;
}


}


class 2:
import

java.util.*;

class

School {


//Array List of teachers
static ArrayList<Teacher> teachers = new ArrayList<Teacher>();



//Adds a teacher to the Array List
publicvoid addTeacher(){
Teacher teacher = new Teacher();
teacher.getData();
teachers.add(teacher);
}



//Returns the amount of test number of a certain teacher
publicint getTeacherTest(String teachName){
Teacher teacher = new Teacher();
int value = 0;
for(int i = 0; i < teachers.size(); i++){
teacher = teachers.get(i);
if(teacher.getName().equals(teachName))
value = teacher.getTest();
}


return value;
}


}


Class 3:
import

java.util.*;

public

class Application {

publicstaticvoid main(String[] arguments){


int choice = 0;



while(choice !=3){
System.out.println("1. Add a teacher");
System.out.println("2. Get the test number");
System.out.println("3. Quit");
System.out.print("Choose an option: ");


Scanner indata = new Scanner(System.in);
choice = indata.nextInt();



//Creates a new school object
School school =

new School();


switch(choice){


case 1:
school.addTeacher();
System.out.println("Teacher added.");
System.out.println();
break;



case 2:
System.out.print("Enter the teacher's name: ");
indata.nextLine();
String teaName = indata.nextLine();
int test = school.getTeacherTest(teaName);
System.out.println("The test amount is: " +test);
System.out.println();
break;


}
}


}


}


Output:
1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 1
Enter the amount: 1
Enter the name of teacher 1:

John

Enter the amount of test:

4

Teacher added.



1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 2
Enter the teacher's name: John
The test amount is: 0



1. Add a teacher
2. Get the test number
3. Quit
Choose an option: 3


Why is it saying "The test amount is: 0" in the output?
When I used the method getTeacherTest() from the school class?
Shouldn't it return the value "4" instead of "0"?
Is there a way to compare the actual values of Strings?
If so, can you please show me how?
Thanks.




#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
case 2:
System.out.print("Enter the teacher's name: ");
[B][COLOR="Red"]indata.nextLine();[/COLOR][/B]
String teaName = indata.nextLine();
int test = school.getTeacherTest(teaName);
System.out.println("The test amount is: " +test);
System.out.println();
break;
What's the reason of sometimes doing a nextline()?

Anyway, that's probably not your biggest issue. Yours is that you recreate a new School object every loop.
//Creates a new school object
School school =


new School();
Move that to before the while-loop.

#3
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
It still doesn't work.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Okay.. Then it wasn't the school's position but in the teacher.getData() method:
for(int num = 1; num <= amount; num++){
System.out.print("Enter the name of teacher " +(num)+": ");
[B][COLOR="blue"]tName = indata.nextLine();[/COLOR][/B]   <<<<-------
[B]i[COLOR="blue"]ndata.nextLine();[/COLOR]  [/B]               <<<<------
System.out.print("Enter the amount of test: ");
testNum = indata.nextInt();

the 2 bold, blue lines should switch positions.

#5
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
It works now but if I add multiple teachers it returns the same test amount for each of them, even though they are different. Do you know how to fix this?

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
If you're adding multiple teachers by entering a higher number when it asks "Enter the amount" it's pretty logical. If i add multiple teachers by doing add teacher multiple times it works fine.

I wasn't really sure what the amount was doing there, if it ain't intended then you should move the "Enter the amount" question and the corresponding for-loop to the school class, as that's where you create the teachers.

like
public void addTeacher() {
        int amount;

        Scanner indata = new Scanner(System.in);
        System.out.print("Enter the amount: ");
        amount = indata.nextInt();

        for (int num = 1; num <= amount; num++) {
            Teacher teacher = new Teacher();
            teacher.getData();
            teachers.add(teacher);
        }
    }


#7
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
I don't know why but its still returning the same test amount for each teacher even with your improvement.