Java代码中的TypeMismatch异常

时间:2018-05-24 19:28:16

标签: java

我得到的错误是

  

在变量' first'"处输入不匹配的内容。

首先是整数。请帮我弄清楚问题

import java.util.*;
public class Hello {

    public static void main(String[] args) {

       String ip,firstpart,lastpart;
       int first,last,strlen,newvar;
       Scanner scanner=new Scanner(System.in);


       first=scanner.nextInt();
       if(scanner.hasNextInt()){
           last=scanner.nextInt();
       }
       else 
           last=0;
       ip=scanner.nextLine();
       firstpart=ip.substring(first);
       strlen=firstpart.length();
       newvar=strlen-last;
       lastpart=firstpart.substring(0,newvar);
       System.out.println(lastpart);

    }
}

1 个答案:

答案 0 :(得分:-1)

first=scanner.nextInt();

将以上代码替换为以下代码

first=Integer.parseInt(scanner.next());