I tried a for loop with an if in it but I didn't know what I should've put in if(?), so then I went for using try and a catch and imported InputMismatchException. I then tried making an array with numbers from 0 to 9(I am a noob and don't know what I am doing) and used a for loop with and if, I know I could use a while. Please help me recent them from entering a string instead of numbers.
import java.util.Scanner;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Employee {
// fields
private String firstName, lastName;// creates both firstName and lastName as string and private fields
private String sal;// creates sal a private and double field
private int years;// creates years a private and int field
// constructors
public Employee() {// default constructor
this("", "", 0, 0);// this sets the default values for each one of the fields
}
public Employee(String firstName, String lastName, double sal, int years) {// overloaded constructor
this.firstName = firstName;// gets the values from the field firstname and then puts it in string firstname
this.lastName = lastName;// gets the value from the field secondname and then puts it in string
// secondname
this.sal = sal;// gets the value from the field sal and then puts it in float sal
this.years = years;// gets the value from the field years and then puts it in int years
}
// methods
public String getFirstName() {// accessor for the field firstname
return firstName;
}
public void setFirstName(String firstName) {// mutator for the field firstname
this.firstName = firstName;
}
public String getLastName() {// accessor for the field lastname
return lastName;
}
public void setLastName(String lastName) {// mutator for the field lastname
this.lastName = lastName;
}
public String getSal() {// accessor for the field lastname
return sal;
}
public void setSal(String sal) {// mutator for the field sal
this.sal = sal;
}
public int getYears() {// accessor for the field years
return years;
}
public void setYears(int years) {// mutator for the field years
this.years = years;
}
public void ScannerMethod() {
int arrInt[] = new int[10];
arrInt[0] = 0;
int j = 0;
boolean a = false;
Scanner get = new Scanner(System.in);
System.out.println("enter first name: ");
firstName = get.next();
System.out.println("enter second name: ");
lastName = get.next();
System.out.println("enter the salary: ");
while(j<10) {
arrInt[j] = j+1;
j++;
}
for(int i = 0; i<1; i++){
if(sal == arrInt[]){
System.out.println("Pleas enter it agian: ");
sal = get.next();
i = 0;
}else{
i = 2;
}
}
/*/while (!a) {
try {
sal = get.nextDouble();
a = false;
} catch (Exception e) {
System.out.println("Invalid input please enter a numeric value: ");
a = true;
}
}/*/
System.out.println("enter the years of service: ");
years = get.nextInt();
}
public String typeStats() {// this method prints out the stats for the employee
System.out.println("Report: " + firstName.substring(0, 1).toUpperCase() + firstName.substring(1) + " "
+ lastName.substring(0, 1).toUpperCase() + lastName.substring(1) + " $" + sal + " " + years);
return null ;
}
}