在一行中扫描不同类型的变量

时间:2014-05-12 02:27:13

标签: java string java.util.scanner

正如标题所说,我想使用用户的一个输入扫描整行输入。输入应该像“Eric 22 1”。

如果nextString()不应该以这种方式使用,我应该使用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");
        }
    }
}

添加和编辑行后:

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]);

我想添加if(name instanceof String)。我很久没碰过Java了,我忘记了使用instanceof的方式,还是错了? 关键是我要比较输入var是int还是string或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")
}

这些线是否有效?

3 个答案:

答案 0 :(得分:0)

  

请输入您的姓名,年龄和性别

因为您需要按特定顺序插入值。


使用nextLine()并执行split

例如:"Abc 123 true 12.5 M"

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

你会有

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

将它们解析为所需的类型。

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

正如您的代码所示,而David表示您可以更改

    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

答案 1 :(得分:0)

当我们使用scanner时,我们没有一个名为nextString()的方法

所以我们必须使用next()来读取字符串。

其次,当你想要阅读整行时,请使用nextLine(),它将以文本的形式读取整行并将其放入字符串中。

现在根据分割字符String entire line可以是split(假设在我们的情况下它是空格)

然后将string arrayparse每个元素设置为所需类型。

如果我们在解析时使用try/catch,那么我们可以捕获输入的不需要的格式的异常并将其抛给用户。

示例代码没有try / catch但您可以根据需要使用try / catch

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]);

答案 2 :(得分:0)

重做这一切,这是代码的重制,以防人们遇到与我相同的问题。 应该将delcaration中的int更改为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");
        }
    }
}  

}