terminate a do...while loop using a string

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 28894 Views - Last Post: 02 September 2013 - 08:37 PM Rate Topic: -----

#1 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

terminate a do...while loop using a string

Posted 25 August 2013 - 07:07 AM

//Author: Darren Daniels
//Purpose: figure out payroll
import java.util.Scanner; //program uses class Scanner

//declare class
public class payrollprogrampart2//begin class
{
	public static void main(String[] args)//Main part of program
	{
	boolean repeat=true;//sentenal veriable
	String getInfo;		
	Scanner input= new Scanner(System.in);//allows for input
		do  //loop till the user is done and enters stop
		{
		System.out.println();//prints blank line
		System.out.println();//prints blank line
		System.out.println("What is the employee's name? (enter stop to exit.)");	//prompt for name	
		String eName=input.nextLine();//input employee's name
		System.out.println();//prints blank line
		System.out.println("How much do they get paid per hour?");// prompt for employee's hourly wage
		float hourlyRate=input.nextFloat();//reads employee's wage inputed
		System.out.println();//prints blank line
		System.out.println("How many hours did the employee work?");//prompt for employee's hours worked 
		float hoursWorked=input.nextFloat();//reads employee's hours inputed
		System.out.println();//prints blank line
		System.out.printf(eName);//displays employee's name
		System.out.printf(" makes $%.2f",(hourlyRate*hoursWorked));//displays what employee makes
		System.out.printf(" per week.");//displays per week
		System.out.println();//prints blank line
		
		}while(!eName.equalsCase("stop")); //end loop
	} //end main
} // end class payrollprogrampart1



I am trying to terminate the loop by having the user input "stop" as the employee name (eName). I have tried many different ways of doing this, but continue to get the following error:>

"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" payrollprogrampart2.java
payrollprogrampart2.java:32: error: cannot find symbol
}while(!eName.equalsCase("stop")); //end loop
^
symbol: variable eName
location: class payrollprogrampart2

1 error

> Terminated with exit code 1.

not sure what to do next.

Is This A Good Question/Topic? 0
  • +

Replies To: terminate a do...while loop using a string

#2 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 86
  • View blog
  • Posts: 169
  • Joined: 27-May 13

Re: terminate a do...while loop using a string

Posted 25 August 2013 - 07:23 AM

while(!eName.equalsCase("stop"));

Here eName, isn't defined.

You create the string eName inside your do {}, but it doesn't exist outside the loop.
So the while doesn't know what eName is.

Why do have a variable getInfo?
You create it, but don't use it anymore.

This post has been edited by axel1994: 25 August 2013 - 07:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: terminate a do...while loop using a string

Posted 25 August 2013 - 07:30 AM

And it's equalsIgnoreCase

But your strategy won't work - the value is checked too late

This post has been edited by g00se: 25 August 2013 - 07:32 AM
Reason for edit:: Clarification

Was This Post Helpful? 2
  • +
  • -

#4 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

Re: terminate a do...while loop using a string

Posted 25 August 2013 - 07:40 AM

View Postaxel1994, on 25 August 2013 - 07:23 AM, said:

while(!eName.equalsCase("stop"));

Here eName, isn't defined.

You create the string eName inside your do {}, but it doesn't exist outside the loop.
So the while doesn't know what eName is.

Why do have a variable getInfo?
You create it, but don't use it anymore.


//Author: Darren Daniels
//Date Modified: 8/23/2013
//Purpose: figure out payroll
import java.util.Scanner; //program uses class Scanner

//declare class
public class payrollprogrampart2//begin class
{
	public static void main(String[] args)//Main part of program
	{
	boolean repeat=true;//sentenal veriable
	String eName;		
	Scanner input= new Scanner(System.in);//allows for input
		do  //loop till the user is done and enters stop
		{
		System.out.println();//prints blank line
		System.out.println();//prints blank line
		System.out.println("What is the employee's name? (enter stop to exit.)");	//prompt for name	
		eName=input.nextLine();//input employee's name
		System.out.println();//prints blank line
		System.out.println("How much do they get paid per hour?");// prompt for employee's hourly wage
		float hourlyRate=input.nextFloat();//reads employee's wage inputed
		System.out.println();//prints blank line
		System.out.println("How many hours did the employee work?");//prompt for employee's hours worked 
		float hoursWorked=input.nextFloat();//reads employee's hours inputed
		System.out.println();//prints blank line
		System.out.printf(eName);//displays employee's name
		System.out.printf(" makes $%.2f",(hourlyRate*hoursWorked));//displays what employee makes
		System.out.printf(" per week.");//displays per week
		System.out.println();//prints blank line
		
		}while(eName!=("stop")); //end loop
	} //end main
} // end class payrollprogrampart1



Must have been looking at it too long...I didn't even see that. Thank you. I fixed it and after running it once entered stop and got the following message:

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" payrollprogrampart2


What is the employee's name? (enter stop to exit.)
Daniels

How much do they get paid per hour?
1

How many hours did the employee work?
1

Daniels makes $1.00 per week.


What is the employee's name? (enter stop to exit.)

How much do they get paid per hour?
stop
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextFloat(Scanner.java:2388)
at payrollprogrampart2.main(payrollprogrampart2.java:22)

> Terminated with exit code 1.
Was This Post Helpful? 0
  • +
  • -

#5 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 86
  • View blog
  • Posts: 169
  • Joined: 27-May 13

Re: terminate a do...while loop using a string

Posted 25 August 2013 - 08:21 AM

You get that error because of this line:
float hourlyRate=input.nextFloat();


Just try to manually follow what your program does.
it asks for a name, you input a string/text and save it in eName.

Finally it asks for:
How much do they get paid per hour?

But your next line in code is:
float hourlyRate=input.nextFloat();


So your program expects a float (number), not a string.
So you get a error.

By this same logic.
You can only exit the program at the following line:
System.out.println("What is the employee's name? (enter stop to exit.)");


Why would that be?
Well, the only time you modify eName is at that point in the program.


A program runs line per line.
It doesn't do:
ow a line of code, perhaps we needs to execute that one line we did 10 lines ago again :P

This post has been edited by axel1994: 25 August 2013 - 08:23 AM

Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: terminate a do...while loop using a string

Posted 25 August 2013 - 09:42 AM

As g00se mentionned the check will be too late
you prompt for a lot of things before performinning the while

for that type of things, use an infinite loop and break out of it

    while(true)
       String name = scanner.nextLine();
       if(name.equalsIgnoreCase("stop"))
          break;
       ...
       ...
     }


Was This Post Helpful? 3
  • +
  • -

#7 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

Re: terminate a do...while loop using a string

Posted 28 August 2013 - 09:03 PM

[code}
//Author: Darren Daniels
//Date Modified: 8/23/2013
//Purpose: figure out payroll
import java.util.Scanner; //program uses class Scanner

//declare class
public class payrollprogrampart2//begin class
{
public static void main(String[] args)//Main part of program
{
String eName;
float hourlyRate;
float hoursWorked;
Scanner input= new Scanner(System.in);//allows for input

do //loop till the user is done and enters stop
{
System.out.println();//prints blank line
System.out.println();//prints blank line
System.out.println("What is the employee's name? (enter stop to exit.)");//prompt for name
eName=input.nextLine();//input employee's name

System.out.println();//prints blank line
System.out.println("How much do they get paid per hour?");// prompt for employee's hourly wage
hourlyRate=input.nextFloat();//reads employee's wage inputed

System.out.println();//prints blank line
System.out.println("How many hours did the employee work?");//prompt for employee's hours worked
hoursWorked=input.nextFloat();//reads employee's hours inputed

System.out.println();//prints blank line
System.out.printf(eName);//displays employee's name
System.out.printf(" makes $%.2f",(hourlyRate*hoursWorked));//displays what employee makes
System.out.printf(" per week.");//displays per week
System.out.println();//prints blank line
input.nextLine();
}while(eName!="stop"); //end loop
} //end main
} // end class payrollprogrampart2
{/code]

I got the program to run the loop with out skipping any inputs. How ever when I input stop for the name it does not end the program.
Was This Post Helpful? 0
  • +
  • -

#8 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: terminate a do...while loop using a string

Posted 28 August 2013 - 09:25 PM

Listen to g00se and pbl. The do while loop checks the value too late. You need to do as pbl suggested in his last post.
Was This Post Helpful? 0
  • +
  • -

#9 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 08:18 AM

This is the start of my code and I am only getting one error and I do have the eName initialized. So what am I over looking?
Scanner input= new Scanner(System.in);//allows for input
float hourlyRate;	
float hoursWorked;
String eName;	
     while (eName!="stop")  //loop till the user is done and enters stop
     {......
     }




---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" payrollprogrampart2.java
payrollprogrampart2.java:16: error: variable eName might not have been initialized
while (eName!="stop") //loop till the user is done and enters stop
^
1 error

> Terminated with exit code 1.
Was This Post Helpful? 0
  • +
  • -

#10 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 08:20 AM

You're overlooking pbl's post explaining exactly what to do.

Also, Strings are not compared with = or !=. use String.equals(str) or !String.equals(str)

This post has been edited by schutzzz: 31 August 2013 - 08:21 AM

Was This Post Helpful? 1
  • +
  • -

#11 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 09:51 AM

Ok here is my code up to this point and my captured outcome. What can I do know to get this to work for me?
//Author: Darren Daniels
//Date Modified: 8/23/2013
//Purpose: figure out payroll
import java.util.Scanner; //program uses class Scanner

//declare class
public class payrollprogrampart2//begin class
{
	public static void main(String[] args)//Main part of program
	
	{
		Scanner input= new Scanner(System.in);//allows for input
		float hourlyRate;	
		float hoursWorked;
		String eName;	
			while (true)  
				String eName=scanner.nextLine();
				if (eName.equalsIgnoreCase("stop"))
					break;//loop till the user is done and enters stop
			{
				
				System.out.println();//prints blank line
				System.out.println();//prints blank line
				System.out.println("What is the employee's name? (enter stop to exit.)");//prompt for name	
				eName=input.nextLine();//input employee's name
				
				System.out.println();//prints blank line
				if (hourlyRate<0)
				System.out.println("How much do they get paid per hour?");// prompt for employee's hourly wage
				hourlyRate=input.nextFloat();//reads employee's wage inputed
								
				System.out.println();//prints blank line
				if (hoursWorked<0)
				System.out.println("How many hours did the employee work?");//prompt for employee's hours worked 
				hoursWorked=input.nextFloat();//reads employee's hours inputed
								
				System.out.println();//prints blank line
				System.out.printf(eName);//displays employee's name
				System.out.printf(" makes $%.2f",(hourlyRate*hoursWorked));//displays what employee makes
				System.out.printf(" per week.");//displays per week
				System.out.println();//prints blank line
				input.nextLine();
			}
	} //end main
} // end class payrollprogrampart2



---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" payrollprogrampart2.java
payrollprogrampart2.java:17: error: not a statement
String eName=scanner.nextLine();
^
payrollprogrampart2.java:17: error: ';' expected
String eName=scanner.nextLine();
^
2 errors

> Terminated with exit code 1.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 10:03 AM

You declare String ename at two different palce should be declare only once
	String eName;	     // <----  here
	while (true)  
		String eName=scanner.nextLine();     // <--- and here
		if (eName.equalsIgnoreCase("stop"))
			break;//loop till the user is done and enters stop
		{    <--- no need the { here


and your comment is completly false
	// String eName;
	while (true)  
		String eName=scanner.nextLine();     // here is enough
		if (eName.equalsIgnoreCase("stop"))
			break;  // break out the loop if ename.equals("stop")
		System.out.println();
		


Was This Post Helpful? 1
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 10:06 AM

Quote

while (true) 

The following is preferable:
while (scanner.hasNextLine()) 

Was This Post Helpful? 0
  • +
  • -

#14 darren_daniels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-August 13

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 10:49 AM

I have made the changes and I am still getting the same errors.
Scanner input= new Scanner(System.in);//allows for input
		float hourlyRate;	
		float hoursWorked;
			while (true)  
				String eName=scanner.nextLine();
				if (eName.equalsIgnoreCase("stop"))
					break;//loop till the user is done and enters stop
				System.out.println();


Was This Post Helpful? 0
  • +
  • -

#15 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: terminate a do...while loop using a string

Posted 31 August 2013 - 11:17 AM

While loop requires braces and also your Scanner's object name is input and not scanner.

This post has been edited by ChrisNt: 31 August 2013 - 11:19 AM

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3