3

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 ; 
    }

}
Pshemo
  • 113,402
  • 22
  • 170
  • 242
shaybra
  • 33
  • 1
  • 4
  • 1
    Possibly related: [How to use Scanner to accept only valid int as input](https://stackoverflow.com/q/2912817) – Pshemo Oct 13 '20 at 19:39

2 Answers2

1

Here is a method which asks the user for a double until they input a valid double.

package test2134235;
import java.util.Scanner;

public class ParseDouble {

    public static void main(String[] args) {

        System.out.println("This is your double: " + getDoubleFromKeyboard());
    }

    static Double getDoubleFromKeyboard() {
        Scanner scanner = new Scanner(System.in);
        for (;;) {
            System.out.println("input a Double value");
            try {
                String input = scanner.nextLine();
                Double d = Double.parseDouble(input);
                return d;
            } catch (NumberFormatException e) {
                System.out.println("Sorry, only Double values can be used.");
            }
        }
    }
}
Danny
  • 468
  • 1
  • 3
  • 20
1

You have a comment and an answer that I think address your needs. I wanted to literally answer the question implied in your header: "Trying to stop the user from entering a String into my double in java"

The answer is you can't control what the user types in to the console. What is typed into the console is always going to be characters and read in as a String. It's your job (as the comment and the answer address) to make sure bad input doesn't crash your program. So you need to analyze it and ask "Does this look like it's a double?" and if it does, go ahead and store it in your variable and if not, take some other action like prompting the user to try again.

Michael Welch
  • 1,732
  • 2
  • 18
  • 29
  • https://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input – shaybra Nov 01 '20 at 23:06