Reputation: 21
Fixed a lot of it, getting an error during the myInt declaration, saying it cannot find the parse method. I've added the imports NetBeans mentioned along with it but still nothing. Here's the updated code:
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Salary {
int salary;
int sales;
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
double Salary;
Salary salary = new Salary();
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
Integer myInt = Integer.parse(salesArray[i]);
for(int i=0; i<salesArray.length; i++){
System.out.print(salesArray[i] + " ");
if (Integer.parseInt(salesArray[i]) <= 0) {
System.out.println("Please enter a value greater than zero");}
else {
Salary = (myInt * 0.09) + 200; }
}
}
}
Thanks a lot for all the help, I really appreciate it.
Upvotes: 0
Views: 227
Reputation: 26522
You will likely want to parse the string to an integer before trying to perform mathematical operations on it (less than or equal to, in this case). You may wish to try something like:
import java.util.Scanner;
public class Salary {
double salary;
int sales;
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
Salary salary = new Salary();
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
for(int i=0; i<salesArray.length; i++){
Integer myInt = Integer.parse(salesArray[i]);
System.out.print(myInt + " ");
if (myInt <= 0) {
System.out.println("Please enter a value greater than zero");}
else {
salary = (myInt * 0.09) + 200; }
}
}
}
Your solution checked to see if the string was equal to the integer 0, which it never will be, since it is a string being compared to an integer. Even if you checked salesArray[i].equals("0")
, this would still only mean it exactly equals "0", disregarding equivalent forms like "000" or "0.0". You also stated in your question that you wanted a "less than or equal to" relation, not an "equals" relation. Doing a string comparison will only be true if the string was exactly "0".
Upvotes: 1
Reputation: 597204
The salesArray
is array of strings. The equals
method should take a string, that is: salesArray[i].equals("0")
But the proper way is to use Integer.parseInt(..)
Upvotes: 0