Click to See Complete Forum and Search --> : Please help me with this Java code....


ConfusedWCP
02-09-2009, 10:54 PM
I have written this code and I am having problems, can you please advise what I might be doing wrong.....the input is salary, percentage in decimal format and then #of years no more than 10.....when I receive my output which should be for the number of years I enter I am getting more than just then years I enter could you please advise what I am doing wrong or where I went wrong in my code.......thanks in advance for any help.....

import java.util.Scanner;

public class teacherpay {
public static void main (String [] args) {

double Salary;
double percentIncrease;
int cntr = 1;
double yearWork;
double endRaise;

Scanner reader = new Scanner(System.in);

System.out.print("Enter beginning yearly salary: ");
Salary = reader.nextDouble();
System.out.print("Enter desired % increase per year in decimal format: ");
percentIncrease = reader.nextDouble();
System.out.print("Enter number of years worked: ");
yearWork = reader.nextDouble();

endRaise = (Salary * percentIncrease);
while(yearWork <= 10){
Salary += endRaise;
endRaise = (Salary * percentIncrease);
System.out.println ("Your salary will be $" + Salary + " during year " + cntr);
yearWork++;
cntr++;
}
}
}



:confused:

Fang
02-10-2009, 01:11 AM
Java is not the same as JavaScript!

Khalid Ali
02-11-2009, 02:49 PM
please post the error that you get.

ConfusedWCP
02-11-2009, 09:31 PM
I am not getting an error....what is happening is my input of lets say, I input 4 years the output gives me 7 years instead of just the 4.....thank you in advance for any help you can give me, I am into several books and still can figure this one out...I would appreciate any help you can give me....

criterion9
02-11-2009, 10:54 PM
int cntr = 1;
double yearWork;
double endRaise;

while(yearWork <= 10){
Salary += endRaise;
endRaise = (Salary * percentIncrease);
System.out.println ("Your salary will be $" + Salary + " during year " + cntr);
yearWork++;
cntr++;
}


What you have above in your while loop is saying as long as the input you entered for years worked is less than ten continue looping. This is a logic error if I am understanding what you are wanting it to do.

It sounds like you are wanting the loop to run from 1 to the number you entered. Is this correct or are you wanting the loop to run from the number you enter to 10?

while(cntr <= yearWork)

Or

while( yearWork <= 10)

To check that yearWork is less than 10:
if(yearWork > 10){
//the number entered is greater than 10
}

ConfusedWCP
02-11-2009, 11:18 PM
I tried these that you suggested and none of them worked you are correct, I want it to run from 1 to the number I entered not all the way to 10. Thanks for the response hope you can still help.

criterion9
02-12-2009, 07:35 AM
Then you will need to use:
while(cntr <= yearWork)

Also you are incrementing your yearWork variable in your loop. It shouldn't change if you are using it as a condition in your while statement. Please post what you have now if you want further help because it will do little good for anyone to assume what you have.

ConfusedWCP
02-12-2009, 02:31 PM
Wow! ok it is working up to the years of input, also I have removed the next to last line of code for it to work....

yearWork++;

however now if I put in lets say 12 years it works with that, I don't want it to go past 10how do I stop that or give a message to many years were input. I appreciate your help.

criterion9
02-12-2009, 04:32 PM
You will need to use a conditional statement.

For example:

if(yearWorked > 10){
//display error or whatever
yearWorked = 10; //sets yearWorked to 10 if you wanted to
}


I posted this earlier but you might've missed it since it was at the bottom of the post. :-)

ConfusedWCP
02-12-2009, 11:38 PM
I now have it somewhat working I don't know where I am going wrong....here is the whole program again, I have tried everything you have said and they worked except for the fact that it is now printing the statement "You have entered to many years" for each line and it doesn't stop at 10 it still keeps going see what you think I guess I am missing something, I have tried fighting with this thing all day. I really appreciate all of your help it has been great...thanks again...

*******

import java.util.Scanner;

public class teacherpay {
public static void main (String [] args) {

double Salary;
double percentIncrease;
int cntr = 1;
double yearWork;
double endRaise;

Scanner reader = new Scanner(System.in);

System.out.print("Enter beginning yearly salary: ");
Salary = reader.nextDouble();
System.out.print("Enter desired % increase per year in decimal format: ");
percentIncrease = reader.nextDouble();
System.out.print("Enter number of years worked: ");
yearWork = reader.nextDouble();

endRaise = (Salary * percentIncrease);
while(yearWork >= cntr){
Salary += endRaise;

{
if (cntr <= yearWork);{
System.out.println ("Your salary will be $" + Salary + " during year " + cntr);
//yearWork++;
cntr++;
}
if(yearWork > 10);{
System.out.println ("You have entered to many years.");
}
}
}
}
}

criterion9
02-13-2009, 07:31 AM
This whole section is out of order:

endRaise = (Salary * percentIncrease);
while(yearWork >= cntr){
Salary += endRaise;

{
if (cntr <= yearWork);{
System.out.println ("Your salary will be $" + Salary + " during year " + cntr);
//yearWork++;
cntr++;
}
if(yearWork > 10);{
System.out.println ("You have entered to many years.");
}

It sounds like you want check that the number of years are correct before you enter the while loop.

In order to get a new value endRaise each time the loop cycles you would need to put the endRaise=Salary*percentIncrease inside the loop.

Your if(cntr <= yearWork) and its entire execution block should also go inside your loop or it will only be executed once.

It may help to get out a piece of paper and step through your code line by line and keep track of all the variables as you go. This should let you know when you might have something out of order or where you are changing a value that you get an unexpected result from during execution.

ConfusedWCP
02-13-2009, 05:36 PM
Thanks I have been out all day...I have to get this in tonight so I will try what you said and hopefully it will work.

Thanks again for all your help and I will take your advise...by writing it out.

Thanks,
ConfusedWCP

ConfusedWCP
02-13-2009, 10:31 PM
Thanks so much again for your help, and I did what you said and it seemed to work after much fustration......

Thanks,
ConfusedWCP:confused:

criterion9
02-14-2009, 09:19 AM
That's the best to understand what it is doing. Practice, practice, practice. :-)