Results 1 to 5 of 5
  1. #1
    drymsza1234 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default Do While with String varification

    Hello I am in need of help. I have an assignment to do that needs to a do while loop to check if "m" or "s" has been typed in.
    Java Code:
    	do													
    	{
    		System.out.println("Enter offence type: ");
    		offenceType = input.next();
    				
    		if(offenceType.equalsIgnoreCase("m"))
    		{
    			offenceFee = minorValue;
    			finalMinorOffences = finalMinorOffences + 1;
    		}
    		else if(offenceType.equalsIgnoreCase("s"))
    		{
    			offenceFee = SERIOUS_FINE;
    			finalSeriousOffences = finalSeriousOffences + 1;
    		}
    		
    		finalFee = finalFee + offenceFee;
    				
    	}while(offenceType != ("m" "s"));
    I know the line is incorrect and it is what I need help with. I need the while to continue until m or s is typed in but dont know how to do so! PLEASE HELP!
    Last edited by Fubarable; 11-08-2009 at 11:10 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One fix: use a boolean for your sentinel variable:
    Java Code:
          boolean inputCorrect = false;
          do {
             System.out.println("Enter offence type (\"m\" / \"s\"): ");
             offenceType = input.next();
    
             if (offenceType.equalsIgnoreCase("m")) {
                offenceFee = minorValue;
                finalMinorOffences = finalMinorOffences + 1;
                
                inputCorrect = true; // set inputCorrect variable here!
             }
             else if (offenceType.equalsIgnoreCase("s")) {
                offenceFee = SERIOUS_FINE;
                finalSeriousOffences = finalSeriousOffences + 1;
                
                inputCorrect = true; // set inputCorrect variable here!
             }
    
             finalFee = finalFee + offenceFee;
    
             //} while(offenceType != ("m" "s"));
          }
          while (!inputCorrect);

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    the other option is to use two calls to equalsIgnoreCase:
    Java Code:
    while (!offenceType.equalsIgnoreCase("m") && !offenceType.equalsIgnoreCase("s"));
    ps: don't forget to use code tags!

    pps: welcome to the forum!

  4. #4
    drymsza1234 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you very much! works like a charm :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're welcome. One more solution (get out your paper and pencil and start doing Venn diagrams!):
    Java Code:
    while(!(offenceType.equalsIgnoreCase("m") || offenceType.equalsIgnoreCase("s")));

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  2. Stuck on Boolean varification--plz help
    By Cutter in forum New To Java
    Replies: 7
    Last Post: 08-05-2009, 02:18 PM
  3. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

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