New to JAVA need some help
Im currently in a JAVA class in college and we had to build a program that calculated commission. i think im in the ball park here with my code but when i run it i cant get past the second question. Im running it on netbeans can anyone see what i did wrong here? It has to be near the error message because i cant get past that point on the code. Now i do have a string that does not allow letters to be input into the second question im not sure if that is the problem or not either.
Code :
package salesperson;
import java.util.Scanner;
public class Salesperson {
public static void main(String[] args) {
final double salary = 40000.00;
final double commissionRate = 0.05;
final double pricePerSale = 250.00;
boolean addperson = true;
Scanner sc = new Scanner(System.in);
System.out.println ("Calculate your commission and total pay");
System.out.println(" ");
while (addperson == true){
double total = 0;
int currentNumber;
String currentString;
String name;
String yesNo;
boolean exit;
boolean isNumber;
System.out.println ("Enter name: ");
name = sc.next();
System.out.println(" ");
do {
System.out.print ("Enter amount of sales: ");
currentString = sc.next();
isNumber = isANumber(currentString);
if (isNumber == true) {
currentNumber = Integer.parseInt(currentString);
if (currentNumber > 0)
{
total += currentNumber * pricePerSale;
}
} {
System.out.println("ERROR: only enter whole numbers");
}
}
while (isNumber == false);
System.out.println("");
total = total * commissionRate;
System.out.print (name + "Earned");
System.out.printf("$%2.2f",total);
System.out.println(" in commissions and Earned ");
total += salary;
System.out.printf("$%2.2f",total);
System.out.println(" this year");
System.out.println("");
do {
System.out.println("Do you want to check another name y/n : ");
yesNo = sc.next();
System.out.println("");
switch (yesNo) {
case "n":
case "N":
addperson = false;
exit = true;
break;
case "y":
case "Y":
exit = true;
break;
default:
System.out.println ("Enter y or n only");
exit = false;
}
}while (exit == false);
}
}
public static boolean isANumber (String s){
int i =0;
boolean valid = true;
if (s.length()==0);
valid = false;
while (i < s.length()){
if (!Character.isDigit(s.charAt(i))){
valid =false;
break;
}
i++;
}
return valid;
}
}
Re: New to JAVA need some help
Quote:
it i cant get past the second question.
Do you get an error message? Please copy the full text of the error message and paste it here.
Copy all of the console that shows what was printed and what the user entered.
On Windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: New to JAVA need some help
No, no error message just to one i made up in the code. Thats the error message i can not get past i think i have it looped wrong. thats all the code i got :) im also running the program on NetBeans
this is the error message that i made and is in the above code
Code :
{
System.out.println("ERROR: only enter whole numbers");
}
Re: New to JAVA need some help
That's not quite what I think of an error message.
Can you post the console's contents from when the program is executed to show what you are talking about?
Some of the usage of {}s is unusual and could be causing you problems.
You need to use the javac command's -Xlint option for warnings. For example:
D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java
Re: New to JAVA need some help
this is what it looks like when i run it
===================================
run:
Calculate your commission and total pay
Enter name:
mike
Enter amount of sales: 12
ERROR: only enter whole numbers
Enter amount of sales: 1
ERROR: only enter whole numbers
Enter amount of sales: 3
ERROR: only enter whole numbers
Enter amount of sales: 4
ERROR: only enter whole numbers
Enter amount of sales: 5
ERROR: only enter whole numbers
Enter amount of sales: -1
ERROR: only enter whole numbers
Enter amount of sales: a
ERROR: only enter whole numbers
Enter amount of sales:
================================
see how even if i enter a normal number i still get the error code that i made. I want it to only show that Error code if someone trying to enter a negative number or a letter.
Re: New to JAVA need some help
Did you see these from my last post:
Some of the usage of {}s is unusual and could be causing you problems. Proper formatting makes the code easier to read and understand.
You need to use the javac command's -Xlint option for warnings. For example:
D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java
Re: New to JAVA need some help
Wiz, you may not notice this but you've coded it so it always gives you ERROR: only enter whole numbers.
Have you thought of using the Math.floor(double n) function? It will only round it to the biggest whole number because it will almost never be just a plain whole number so it rounds it for you in the program, and if it still doesn't work for you just edit your code for us to see.
I'm sure this is intentional because it doesn't seem done yet but Its forever looping once you reach the second question.
Edit: Compiler doesn't matter, Eclipse still has your problem
Re: New to JAVA need some help
Norm I think i get what you are saying about javac commands but that is not the problem since im Using NetBeans Program to build the code and also to run the code therefor no need for command prompt or javac but i am very confused on the {} symbols im not to sure where to place them. i tried to get rid of a few but then i will get syntax errors for not having a closed loop.
shadowhoundz where would i enter Math.floor(double n) sorry still very new at this
Re: New to JAVA need some help
Quote:
that is not the problem
I think if you were to use the -Xlint option to get the compiler's warning message, it would help you fix part of the problem.
The poorly positioned {}s make the code hard to read and understand, but that does not change how the code will compile and execute. The compiler doesn't care too much about formatting.
Re: New to JAVA need some help
oh ok ill try to clean up the code {}'s to make it easer to read. what is -xlint and i think one problem is that it keeps looping anyidea how to stop it from looping back to the Error message i have made?
Re: New to JAVA need some help
You need to use the javac command's -Xlint option for warnings. For example:
D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java
I have no idea how to tell an IDE how to use that javac option.
Quote:
how to stop it from looping back
What condition in the loop keeps the loop looping? What code sets that condition?
Re: New to JAVA need some help
This is where im having an issue with it just looping the error message at the bottom in the code
while (addperson == true){
double total = 0;
int currentNumber;
String currentString;
String name;
String yesNo;
boolean exit;
boolean isNumber;
System.out.println ("Enter name: ");
name = sc.next();
System.out.println(" ");
do {
System.out.print ("Enter amount of sales: ");
currentString = sc.next();
isNumber = isANumber(currentString);
if (isNumber == true) {
currentNumber = Integer.parseInt(currentString);
if (currentNumber > 0)
{
total += currentNumber * pricePerSale;
}
} {
System.out.println("ERROR: only enter whole numbers");
}
}
while (isNumber == false);
Re: New to JAVA need some help
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Where controls the value of isNumber?
Re: New to JAVA need some help
Code java:
package salesperson;
import java.util.Scanner;
public class Salesperson {
public static void main(String[] args)
{
final double salary = 40000.00;
final double commissionRate = 0.05;
final double pricePerSale = 250.00;
boolean addperson = true;
Scanner sc = new Scanner(System.in);
System.out.println ("Calculate your commission and total pay");
System.out.println(" ");
while (addperson == true){
double total = 0;
int currentNumber;
String currentString;
String name;
String yesNo;
boolean exit;
boolean isNumber;
boolean valid;
System.out.println ("Enter name: ");
name = sc.next();
System.out.println(" ");
do {
System.out.println ("Enter amount of sales: ");
currentString = sc.next();
System.out.println (" ");
isNumber = isANumber(currentString);
if (isNumber == true)
{
currentNumber = Integer.parseInt(currentString);
if (currentNumber > 0)
{
total += currentNumber * pricePerSale;
}
}
{
System.out.println("=========== ERROR: only enter whole numbers ===========");
}
}
while (isNumber == false);
System.out.println("");
total = total * commissionRate;
System.out.print (name + "Earned");
System.out.printf("$%2.2f",total);
System.out.println(" in commissions and Earned ");
total += salary;
System.out.printf("$%2.2f",total);
System.out.println(" this year");
System.out.println("");
do {
System.out.println("Do you want to check another name y/n : ");
yesNo = sc.next();
System.out.println("");
switch (yesNo) {
case "n":
case "N":
addperson = false;
exit = true;
break;
case "y":
case "Y":
exit = true;
break;
default:
System.out.println ("Enter y or n only");
exit = false;
}
} while (exit == false);
}
}
public static boolean isANumber (String s){
int i =0;
boolean valid = true;
if (s.length()==0);
valid = false;
while (i < s.length())
{
if (!Character.isDigit(s.charAt(i)))
{
valid =false;
break;
}
i++;
}
return valid;
}
}
Re: New to JAVA need some help
Have you fixed the problem? I don't see any comments in the last post.
I'm done for tonight. Back tomorrow.
Re: New to JAVA need some help
nope still not working and still unsure of what i have done to cause this lol. i guess this happends when you are new to this type of thing. anyone else see whats happening? look at previous post please.
Re: New to JAVA need some help
Try compiling with the javac command using the -xlint option to get useful warning messages.
What controls the value of isNumber?
Re: New to JAVA need some help
I see his problem, he has no values to go to the next question, its forever looping because his variable stays the same