Results 1 to 2 of 2
  1. #1
    grim is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default 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
    Java 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
    Java 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with the tostring class

    Java 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.

    Java Code:
    if (values[0].toLowerCase().equals("volunteer")) {
    In fact the String class has another method - equalsIgnoreCase() - that may (or may not) be useful.

    -----

    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.

Similar Threads

  1. toString
    By Waarten in forum New To Java
    Replies: 11
    Last Post: 01-25-2012, 04:02 PM
  2. toString
    By juanvalentinequintana in forum New To Java
    Replies: 2
    Last Post: 05-13-2011, 05:30 PM
  3. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 03:44 PM
  4. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 09:52 AM
  5. Replies: 2
    Last Post: 10-23-2009, 03:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •