Learning the basics... Some help plz!

I am currently attending IT class about Java Programming, and coming from the nooblet point of view... this is kinda crazy... i like this though... but i need some help to get this point through...

I am doing something wrong... i know... but I got no clue what it is... logically to me, it seems correct... and I would code it more or less the same way in php... but for some reason, it is not behaving accordingly in here with JAVA...

I am taking the employeeName declaration outside the loop and using it as the loop breaker... I am declaring it as String employeeName = ""; then comes the do-while loop and the while expression states that employeeName != "stop" so it continues to loop until employeeName equals stop.

It IS looping... HOWEVER, it is jumping one of the input.nextLine(); i got for the name input... at the very top of the do-while... I have tried many things and I am yet to see a change in this... i dont know what is going on with it either... I have read all the text, but i cannot find the same kind of application i am putting in here because I have a string variable check situation... nothing in the text uses that ... there was one case that they used the end-of-file command to break the loop... but the assignment is asking for stop INPUT to break the loop... so I am trying hard to find a way of making this happen... Still... i got no clue what is going on with it... here is my code up to this point:

package payrollpart1;

import java.util.Scanner;

publicclass Main{

publicstaticvoid main(String[] args){

Scanner input =new Scanner(System.in);

String employeeName ="";

do{

System.out.println("\n\nWhat is the name of the Employee?\n(Enter stop to quit)");

employeeName = input.nextLine();

System.out.println("\n\nWhat is the hourly rate of the employee?");

double Hrate = input.nextDouble();

System.out.println("\n\nHow many hours did this employee work?\n(State fractions of hours as decimals. an hour and a half like 1.5)");

double Hamount = input.nextDouble();

double salary = Hrate * Hamount;

System.out.printf("\n\n\n+-+\n"+

"|Payroll Report for: %s\n"+

"Hourly Rate: $%.2f\n"+

"|Total Hours this Week: %.2f\n"+

"|Gross Salary: $%.2f\n"+

"+-+\n\n"

,employeeName,Hrate,Hamount,salary);

}while(employeeName !="stop");

}

}

}

This is my code more or less... i ommited the comments, but i am sure some of you wont be needing them anyway... I hope someone can help me out with this thing... Thanks in advance!

[3515 byte] By [Elvenelfa] at [2007-11-27 7:51:13]
# 1
When comparing Strings (or other types of objects) don't use == or !=,which only compare memory locations. Instead use method equals:if (a.equals(b))...if (! a.equals(b))
Hippolytea at 2007-7-12 19:32:19 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks! I will give this a try.

EDIT:

Ok i did it, and it breaks the loop, but it is still omitting the employeeName input upon first loop. So if I do not enter stop as the employeeName in the first chance, when it loops i dont get to enter employeeName again because it will skip right into hourly rate... what did I do wrong!?

Message was edited by:

Elvenelf

Elvenelfa at 2007-7-12 19:32:19 > top of Java-index,Java Essentials,New To Java...
# 3

nextDouble reads a double but doesn't read and discard the <ENTER> you

typed after the number. Then nextLine is called and it reads the employee name as an empty String.

To fix this, the best thing to do is discard the rest of the line after any call to nextDouble:

double Hamount = input.nextDouble();

input.nextLine(); //discard rest of line

Hippolytea at 2007-7-12 19:32:19 > top of Java-index,Java Essentials,New To Java...
# 4

Awesome! That is what I had wrong!

Ok, so I must use input.nextLine() after any input.nextDouble()... makes sense now that I know this, thats why it was jumping right into the next input... kinda funky the way it works huh... if I was the developer of the language Id look to improve that lol, but I started last week, so I can't ask for much hehe.

Your tips are much appreciated!

Elvenelfa at 2007-7-12 19:32:19 > top of Java-index,Java Essentials,New To Java...