Marcus Burkhart
Marcus Burkhart

Reputation: 93

Prompt user to enter input using Scanner

I am trying to accept 4 integer values, 4 float values, and 3 characters in the order listed below. I am having trouble accepting characters and keep running into errors no matter what I try and type in. I have tried both char_value1 = charval.next(); and also this char_value1 = charval.nextChar(); what am I doing wrong and how can I change this to work correctly?

import java.util.Scanner;


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

        // Four integer values
        int int_value1;
        int int_value2;
        int int_value3;
        int int_value4;

        // Four double values
        double dbl_value1;
        double dbl_value2;
        double dbl_value3;
        double dbl_value4;

        // Three character values
        char char_value1;
        char char_value2;
        char char_value3;

       // Number value scanner here
       Scanner numval = new Scanner(System.in);

       // Prompt user to enter 4 integer values
       System.out.println("Enter 4 integer values below with a space in between");

       // 4 integer values set in order as they were entered
       int_value1 = numval.nextInt();
       int_value2 = numval.nextInt();
       int_value3 = numval.nextInt();
       int_value4 = numval.nextInt();

       // Prompt user to enter 4 float values
       System.out.println("\nEnter 4 floating values below with a space in between");

       // 4 float values set in order as they were entered
       dbl_value1 = numval.nextDouble();
       dbl_value2 = numval.nextDouble();
       dbl_value3 = numval.nextDouble();
       dbl_value4 = numval.nextDouble();

       numval.close();

       // Character value scanner here
       Scanner charval = new Scanner(System.in);

       System.out.println("\nEnter any 3 characters below with a space in between");

       char_value1 = charval.nextChar();
       char_value2 = charval.nextChar();
       char_value3 = charval.nextChar();

       charval.close();

Upvotes: 2

Views: 1311

Answers (1)

DevAB
DevAB

Reputation: 373

Multiple Scanner are not necessary you can take all inputs using one scanner and use next().chatAt(0) to get character inputs

here is your working code

 import java.util.Scanner;


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

            // Four integer values
            int int_value1;
            int int_value2;
            int int_value3;
            int int_value4;

            // Four double values
            double dbl_value1;
            double dbl_value2;
            double dbl_value3;
            double dbl_value4;

            // Three character values
            char char_value1;
            char char_value2;
            char char_value3;

           // Number value scanner here
           Scanner numval = new Scanner(System.in);

           // Prompt user to enter 4 integer values
           System.out.println("Enter 4 integer values below with a space in between");

           // 4 integer values set in order as they were entered
           int_value1 = numval.nextInt();
           int_value2 = numval.nextInt();
           int_value3 = numval.nextInt();
           int_value4 = numval.nextInt();

           // Prompt user to enter 4 float values
           System.out.println("\nEnter 4 floating values below with a space in between");

           // 4 float values set in order as they were entered
           dbl_value1 = numval.nextDouble();
           dbl_value2 = numval.nextDouble();
           dbl_value3 = numval.nextDouble();
           dbl_value4 = numval.nextDouble();

           System.out.println("\nEnter any 3 characters below with a space in between");

           char_value1 = numval.next().charAt(0);
           char_value2 = numval.next().charAt(0);
           char_value3 = numval.next().charAt(0);



}
} 

Upvotes: 1

Related Questions