使用Scanner在简单Java程序中编译错误

时间:2017-03-25 07:34:07

标签: java compilation

import java.util.*;
public class strings {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.next();
        System.out.print("Type your seconds integer : ");
        int second = keyboard.next();
        System.out.print("The sum of your two integers are:");
    }
}

我不知道为什么我在字符串上获得2个错误无法转换为int。

14 个答案:

答案 0 :(得分:12)

keyboard.nextInt(); 

而不是

keyboard.next();

如果您想使用keyboard.next();,请将int first更改为String first

将您的代码修改为:

import java.util.*;
public class Demo {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.nextInt();

        System.out.print("Type your seconds integer : ");
        int second = keyboard.nextInt();

        System.out.print("The sum of your two integers are:" +(first+second));
    }
}

将您的班级名称更改为其他名称,而不是string

答案 1 :(得分:2)

有多种方法可以执行此操作,具体取决于您要执行的验证次数。

在下面的解决方案中,使用.nextInt()获得第一个数字。注意,如果它不是整数,我们需要清除流输入,所以在捕获InputMismatchException时我们会调用.next()。否则,由于输入尚未清除,同样的异常将继续发生

第二个数字是使用.next()获得的,它返回一个字符串,然后我们将该字符串转换为整数。请注意,这里我们捕获一个不同的异常NumberFormatException以确保输入仍然良好。 (我们不会调用.next())

var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)

override func didMoveToView(view: SKView) {
playSound(sound)
//call playSound method when you want
}

func playSound(sound : SKAction)
{
runAction(sound)
}

我做的最后一件事是尝试使用资源,这样输入流将被关闭,无论程序是否正常退出或抛出异常。

答案 2 :(得分:2)

扫描程序类next()方法返回String值。您可以执行以下任何步骤来解决编译错误。

  1. 您需要将String值转换为int。

    int first = Integer.parseInt(keyboard.next());
    
  2. 您可以使用nextInt()方法直接从用户获取int值。

答案 3 :(得分:2)

这将读取整行,然后将其转换为整数

  

int first = Integer.paresInt(keyboard.nextLine());

如果您确定输入数字

  

int first = keyboard.nextInt();

答案 4 :(得分:2)

我有三个解决这个问题的方法:

<强>第一

在您的代码中,您使用keyboard.next(),它将返回一个字符串,您必须将其转换为Integer。为此你可以使用keyboard.nextInt(),它会直接将字符串转换为整数。

import java.util.Scanner;

public class strings {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.nextInt();
        System.out.print("Type your seconds integer : ");
        int second = keyboard.nextInt();
        System.out.print("The sum of your two integers are:"+(first+second));
    }
}

<强>第二

或者您可以使用Integer.parseInt(**String to be converted to Integer**)将字符串转换为整数,这将转换string into Integer

import java.util.Scanner;

public class strings {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = Integer.parseInt(keyboard.next());
        System.out.print("Type your seconds integer : ");
        int second = Integer.parseInt(keyboard.next());
        System.out.print("The sum of your two integers are:"+(first+second));
    }
}

<强>第三

或者您可以使用Integer.valueOf(**String to be converted to Integer**)将字符串转换为整数,这将转换string into Integer

import java.util.Scanner;

public class strings {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = Integer.valueOf(keyboard.next());
        System.out.print("Type your seconds integer : ");
        int second = Integer.valueOf(keyboard.next());
        System.out.print("The sum of your two integers are:"+(first+second));
    }
}

答案 5 :(得分:1)

您可以使用scanner.nextLine()(读取输入到行尾)提供String输出并使用int将其转换为Integer.parseInt,如下所示:

          Scanner keyboard = new Scanner(System.in);
          try {
              System.out.print("Type your first integer: ");
              int first = Integer.parseInt(keyboard.nextLine());
              System.out.print("Type your seconds integer : ");
              int second = Integer.parseInt(keyboard.nextLine());
              int sum  =first+second;
              System.out.print("The sum of your two integers are:"+sum);
          } catch(Exception exe) {
              System.out.println(" Error!!!! Please try again !!!! ");
          } finally {
              keyboard.close();
          }

另外,尝试close资源(上面显示的finally块内)作为练习。

答案 6 :(得分:1)

您正在使用Scanner类中的next()方法。此方法返回String,您不能将字符串存储在int类型变量中。因此,您可以对int类型输入使用nextInt()方法,或者您可以将值存储在String变量中,之后可以将String变量转换为int。

请遵循以下代码:

import java.util.*;
public class strings {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        int first = keyboard.nextInt();
        System.out.print("Type your seconds integer : ");
        int second = keyboard.nextInt();
        System.out.print("The sum of your two integers are:" +(first+second));
    }
}

或遵循此代码

import java.util.*;
public class strings {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Type your first integer: ");
        //Get String input and stroe at String variable
        String firstString = keyboard.next();
        //Convert the String variable to int 
        int first=Integer.parseInt(firstString);
        System.out.print("Type your seconds integer : ");
        //Get String input and stroe at String variable
        String secondString = keyboard.next();
        //Convert the String variable to int 
        int second=Integer.parseInt(secondString);
        System.out.print("The sum of your two integers are:" +(first+second));
    }
}

答案 7 :(得分:1)

正如其他人所说,你可以使用

int first = keyboard.nextInt();

但你也可以使用next()方法和parseInt,如果你愿意,可以有一个显示错误信息的便利好处。

import java.util.*;
public class myClass {
public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Type your first integer: ");
    String firstStr = keyboard.next();
    System.out.println("Type your seconds integer : ");
    String secondStr = keyboard.next();
    int first = -1; //assuming only positive integers
    int second = -1;
     do{
      try{
      first = Integer.parseInt(firstStr);
      second = Integer.parseInt(secondStr);
      int sum = first + second;
      System.out.println("The sum of your two integers are: " + sum);
     } catch (NumberFormatException exception) {
      System.out.println("Integers only.");
     }
  } while (first=-1 && second=-1);
 }
}

答案 8 :(得分:1)

更改您的firstsecond变量
int first = keyboard.next();  
int second = keyboard.next();

int first = keyboard.nextInt();
int second = keyboard.nextInt();

答案 9 :(得分:0)

你应该用nextInt替换next,就像这样

int first = keyboard.nextInt();

从API中,您可以看到next的返回类型是String,而nextInt()是int。

答案 10 :(得分:0)

问题出在这里:

int first = keyboard.next();

Scanner.next返回String,你期望int,所以你需要用

替换它
int first = keyboard.nextInt();

同样

  int second = keyboard.nextInt();

答案 11 :(得分:0)

尝试使用

keyboard.nextInt();

keyboard.next();

它显示它无法转换,因为.next用于字符串,但对于其他数据类型是.next,然后是带有大写的数据类型。例如:

long l=keyboard.nextLong(); 

boolean b=keyboard.nextBoolean();

答案 12 :(得分:0)

keyboard.next()将从扫描程序返回下一个完整的标记,我想到字符串类型所以需要将其转换为整数,更好的选择是使用keyboard.nextInt()方法获取整数输入。 / p>

答案 13 :(得分:0)

您已将代码更改为:

import java.util.*;
public class strings {
    public static void main (String [] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type your first integer: ");
        int first = keyboard.nextInt();
        System.out.println("Type your seconds integer : ");
        int second = keyboard.nextInt();
        System.out.print("The sum of your two integers are:"+(first+second));
    }
}