Question

As the title says, I would like to scan the whole line of input just using one input from user. The input should be like "Eric 22 1".

If nextString() shouldn't be used that way, should I just use hasNext?

JAVA CODE :

import java.util.Scanner;

public class tugas1
{
    public static void main(String []args)
    {
        String name;
        int age;
        boolean sex;

        Scanner sc = new Scanner(System.in);

        System.out.println("Please input your name, age, and sex(input 1 if you are a male, or 0 if you are a female) :");
        name = sc.nextString();
        age = sc.nextInt();
        sex = sc.nextBoolean();

        if(isString(name))
        {
            if(isInteger(age))
            {
                if(isBoolean(sex))
                {
                    System.out.println("Correct format. You are :" +name);
                }
                else
                {
                    System.out.println("Please input the age in integer");
                }
            }
            else
            {
                System.out.println("Please input the age in integer");
            }
        }
        else
        {
            System.out.println("Please input the name in string");
        }
    }
}

After adding and editing the lines :

System.out.println("Please input your name, age, and sex(input 1 if you are a male, or 0 if you are a female) :");
    String input = sc.nextLine();

String[] inputAfterSplit = input.split(" ");

String name = inputAfterSplit[0];
int age = Integer.parseInt(inputAfterSplit[1]);
boolean sex = Boolean.parseBoolean(inputAfterSplit[2]);

I would like to add if(name instanceof String). I haven't touched Java since a long time and I forgot is that the way of using instanceof, or is that wrong? The point is I want to compare if the input var is in int or string or bool.

if(name instanceof String)
{
    if(age instanceof Integer)
    {
        if(sex instanceof Boolean)
        {    
            System.out.println("All checked out")
        }
        else
        {
            System.out.println("Not boolean")
        }
    else
    {
        System.out.println("Not int")
    }

System.out.println("Not string")
}

Will these lines work?

Was it helpful?

Solution

Please input your name, age, and sex

As you need to insert values in specific sequence.


Use nextLine() and perform split

For Example:"Abc 123 true 12.5 M"

String s[]=line.split(" ");

And you will have

s[0]="Abc"
s[1]="123"
s[2]="true"
s[3]="12.5"
s[4]="M"

Than parse them to required type.

String first=s[0];
int second=Integer.parseInt(s[1].trim());
boolean third=Boolean.parseBoolean(s[2].trim());
double forth=Double.parseDouble(s[3].trim());
char fifth=s[4].charAt(0);

As your code suggest and as David said you can change just this

    name = sc.next();//will read next token
    age = sc.nextInt();
    sex = (sc.next()).charAt(0);//change sex to character for M and F
    //or //sex = sc.nextInt();//change it to int

OTHER TIPS

first thing when we use scanner , we dont have a method called nextString()

so instead we must use next() which is to read string.

secondly when you want to read whole line then use nextLine() which will read entire line in the form of text and put it in a string.

now the String which is read as entire line can be split based on split character(assume it is space in our case)

then get the string array and parse each element to required type.

better if we use try/catch while parsing so that we can catch exception for unwanted format for the input and throw it to user.

sample code without try/catch but you use try/catch as per your need

Scanner sc = new Scanner(System.in);

    System.out.println("Please input your name, age, and sex(input 1 if you are a male, or 0 if you are a female) :");
      String input = sc.nextLine();

      String[] inputAfterSplit = input.split(" ");

      String firstParam = inputAfterSplit[0];
      int secondParam=Integer.parseInt(inputAfterSplit[1]);
      boolean thirdParam=Boolean.parseBoolean(inputAfterSplit[2]);

Reworked it all, this is the remake of the code just in case people are having same problem as mine.. int in the delcaration should be changed into Integer

import java.util.Scanner;
import java.lang.*;

public class tugas1
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);

    System.out.println("Input number of line :");
    int lineNum = sc.nextInt();

    String[] name = new String[lineNum];
    Integer[] age = new Integer[lineNum];
    String[] gender = new String[lineNum];

    System.out.println("Please input your name, age, and gender(Male/Female) \n(Separate each line by an enter) :");
    for ( int i = 0; i < lineNum; i++)
    {
        System.out.print("Line " + (i+1) + " : ");
        name[i] = sc.next();
        age[i] = sc.nextInt();
        gender[i] = sc.next();
    }

    for ( int j = 0; j < lineNum; j++ )
    {
        if (name[j] instanceof String)
        {
            if (age[j] instanceof Integer)
            {
                if (gender[j] instanceof String)
                {
                    System.out.println("Person #" + (j+1) +  " is " + name[j] + ", with age of " + age[j] + " years old, and gender " + gender[j]);
                }
                else
                {
                    System.out.println("Gender is missing");
                }
            }
            else
            {
                System.out.println("Age and Gender are");
            }
        }
        else
        {
            System.out.println("Name, Age and Gender are missing");
        }
    }
}  

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top