【问题标题】:How to read characters and integers separately如何分别读取字符和整数
【发布时间】:2018-12-20 10:31:19
【问题描述】:

我必须用 Scanner 读取以下符号并分别处理它们。

输入是:

@@@xx@*
1 -1 -1 4

第一行是游戏动物的生命和食物,第二行是她将-向左移动,+向右移动

我从一些东西开始,但还不够:

Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
while (!sc.hasNext("z")) {
    char ch = sc.next().charAt(0);
    System.out.print("[" + ch + "] "); // to check what is happening
}

如何用-+读取第二行整数然后进行操作?

【问题讨论】:

  • 您应该为此任务使用序列化库
  • 你一次得到输入?还是先获取第一行,然后获取第二行?
  • 只要你能像while (s.hasNext()) {System.out.println(s.nextInt());}那样做
  • 先第一行,后第二行

标签: java regex string java.util.scanner


【解决方案1】:

Java Scanner 类有一大堆方法用于抓取和解析字符串的下一部分,例如next()nextInt()nextDouble()

代码如下所示:

String input = "1 -1 -1 4";
Scanner s = new Scanner(input);
int i1 = s.nextInt();
int i2 = s.nextInt();
int i3 = s.nextInt();
int i4 = s.nextInt();

将读取所有四个值

【讨论】:

    【解决方案2】:

    您可以使用 Scanner 的内置方法,例如 nextInt()next() ,也可以寻找类似 @​​987654323@ 的东西,它会很有用。

    【讨论】:

      【解决方案3】:

      您可以将输入字符解析为整数,如果它解析然后您可以继续您的代码,如果它抛出数字格式异常那么您应该知道输入字符不是数字。

         Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
      
          while (!sc.hasNext("z")) {
              char ch = sc.next().charAt(0);
              try {
                  int a = Integer.parseInt(String.valueOf(ch));
                  switch (a){
                      case 1:
                          // your condition
                      case -1:
                          // your condition
                      case -4:
                          //condition
                      default:
                          // your condition
                  }
              }catch (NumberFormatException ex){
                  System.out.printf("input character is not number");
              }
              System.out.print("[" + ch + "] ");// to chack what is happening
          }
      

      【讨论】:

        【解决方案4】:

        您可以使用各种扫描器类函数来执行此操作。输入是: 1 -1 -1 4

        创建两个数组来存储字符“-”和“+”以及一个存储整数

        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
           if(sc.hasNextInt()){
                 intArray = sc.nextInt();
           }
           else charArray = sc.next();
        }
        

        【讨论】:

          【解决方案5】:

          您可以阅读整行而不是按字符阅读。然后将行拆分为字符串数组,该数组将包含行的字符并使用数组进行操作。这里是您案例的解决方案line.replaceAll("(-*[\\W\\d\\w])(\\s*)", "$1 ")

          public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          while (!sc.hasNext("z")) {
            String line = sc.nextLine();
            String[] split = line.replaceAll("(-*[\\W\\d\\w])(\\s*)", "$1 ").trim().split(" ");
            Arrays.asList(split).forEach(ch -> System.out.print(ch + " "));
          }
          }
          

          输出:

          @@@xx@*
          @ @ @ x x @ * 
          
          1 -1 -1 4 
          1 -1 -1 4 
          

          【讨论】:

            【解决方案6】:

            正如大多数人所说,您可以使用scanner.nextInt() 并查看是否必须向左或向右移动,您有方法Math.signum() 告诉您要走,然后您可以将 Math.abs() 取值作为绝对数。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-11-30
              • 2019-01-17
              • 2018-10-25
              • 1970-01-01
              • 2013-05-24
              • 2021-02-17
              相关资源
              最近更新 更多