Help with the tostring class
Hello I need help, My program does not recognize the input volunteer when I run it. I do not know what is wrong. Please help. Thank you
Main Driver
Code:
import java.io.*; //to use InputStreamReader and BufferedReader
import java.util.*; //to use ArrayList
public class Assignment5
{
public static void main (String[] args)
{
char input1;
String inputInfo = new String();
String line = new String();
boolean found = false;
// ArrayList object is used to store member objects
ArrayList<Object> memberList = new ArrayList<Object>();
try
{
printMenu(); // print out menu
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.println("What action would you like to perform?");
line = stdin.readLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
switch (input1)
{
case 'A':
System.out.print("Please enter a member information to add:\n");
inputInfo = stdin.readLine().trim();
Object addMember = StaffMemberParser.parseStringToMember(inputInfo);
memberList.add(addMember);
break;
case 'C': //Compute Pay
for (int i = 0; i < memberList.size(); i++) {
Object comp = memberList.get(i);
StaffMember member = (StaffMember) comp;
member.computePay();
}
System.out.print("pay computed\n");
break;
case 'D': //Search for Member
System.out.print("Please enter a memberID to search:\n");
inputInfo = stdin.readLine().trim();
for (int i = 0; i < memberList.size(); i++) {
Object search = memberList.get(i);
StaffMember searchMember = (StaffMember) search;
// checks if the member ID matches up with an existing ID
if (searchMember.getMemberId().equals(inputInfo))
found = true;
}
if (found == true)
System.out.print("member found\n");
else
System.out.print("member not found\n");
// refreshes boolean found
found = false;
break;
case 'L': //List Members
if (memberList.size() > 0) {
for (int i = 0; i < memberList.size(); i++) {
Object list = memberList.get(i);
System.out.println(list.toString());
}
}
else
System.out.print("no member\n");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q'); // stop the loop when Q is read
}
catch (IOException exception)
{
System.out.println("IO Exception");
}
}
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd Member\n" +
"C\t\tCompute Pay\n" +
"D\t\tSearch for Member\n" +
"L\t\tList Members\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");
}
code that is giving me problems
Code:
public class StaffMemberParser {
public static StaffMember parseStringToMember(String lineToParse){
String fn;
String ln;
String id;
int hw = 0;
double rate = 0.0;
double bonus = 0.0;
String[] values = lineToParse.split("/");
System.out.println(values[0].toLowerCase());
// First, set all the common attributes
fn = values[1];
ln = values[2];
id = values[3];
if (values[0].toLowerCase() == "volunteer") {
System.out.println(values[0].toLowerCase());
return new Volunteer(fn,ln,id); // Used toLowerCase method to cover all possible cases of spelling
}
else if (values[0].toLowerCase() == "fulltimeemployee"){
rate = Double.parseDouble(values[4]);
bonus = Double.parseDouble(values[5]);
return new FullTimeEmployee(fn,ln,id,rate,bonus);
}
else if (values[0].toLowerCase() == "hourlyemployee"){
rate = Double.parseDouble(values[4]);
hw = Integer.parseInt(values[5]);
return new HourlyEmployee(fn,ln,id,rate,hw);
}
else{
System.out.println("Returned a null");
return null;
}
}
}
There are three other classes, but they are not important. Ask if you need me to post them
Re: Help with the tostring class
Quote:
Code:
if (values[0].toLowerCase() == "volunteer") {
Don't use == to compare instances of a class for equality, because that's not what it does.
Instead use the equals() method.
Code:
if (values[0].toLowerCase().equals("volunteer")) {
In fact the String class has another method - equalsIgnoreCase() - that may (or may not) be useful.
-----
Quote:
My program does not recognize the input volunteer when I run it. ... code that is giving me problems
It is a very good idea to describe a problem - describe what does happen, what is printed, subsequent program behaviour - rather than assert with somewhat undefined terms like "recognised", what does not happen.