相关疑难解决方法(0)

java.util.NoSuchElementException:找不到行

当我通过扫描仪读取文件时,我的程序中出现了运行时异常.

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)    
   at Day1.ReadFile.read(ReadFile.java:49)  
   at Day1.ParseTree.main(ParseTree.java:17) 
Run Code Online (Sandbox Code Playgroud)

我的代码是:

while((str=sc.nextLine())!=null){
    i=0;
    if(str.equals("Locations"))
    {
        size=4;
        t=3;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Professions"))
    {
        size=3;
        t=2;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Individuals"))
    {
        size=4;
        t=4;
        str=sc.nextLine();
        str=sc.nextLine();
    }

int j=0;
String loc[]=new String[size];
while(j<size){
    beg=0;
    end=str.indexOf(',');
    if(end!=-1){
        tmp=str.substring(beg, end);
        beg=end+2;
    }
    if(end==-1)
    {
        tmp=str.substring(beg);
    }
    if(beg<str.length())
        str=str.substring(beg);
    loc[i]=tmp;
    i++;

    if(i==size ){
        if(t==3)
        {
            location.add(loc);
        }
        if(t==2)
        {
            profession.add(loc);
        }
        if(t==4)
        {
            individual.add(loc);
        }
        i=0;
    }
    j++;
    System.out.print("\n");
}
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

30
推荐指数
5
解决办法
16万
查看次数

划分(6.022/6.022 = 1000)Java时获得1000作为答案而不是1

所以,我是编程的新手,我试图制作一个基本的鼹鼠(化学)计算器只是为了好玩.我没有找到这个问题.如果有人回答,请发给我链接.

这是公式:n = N / Nawhere n = moleNa = 6.022E23

代码的第一部分抛出错误.只是试图得到一个,用我的给定N除以Na,甚至用6.022而不是6.022E23我得到1000.0作为答案.

Scanner in = new Scanner(System.in);
double Na = 6.022;

System.out.print("What do you want to know? Mol(0) or N(1)? ");
int first = in.nextInt();
if (first == 0){
    System.out.print("Insert N: ");
    double N = in.nextDouble();
    double mol = N/Na;
    System.out.print("There are " + mol + " mol in that sample.");
}
else if (first == 1){
    System.out.print("Insert mol: ");
    double mol = in.nextDouble();
    double N …
Run Code Online (Sandbox Code Playgroud)

java integer-division

6
推荐指数
1
解决办法
191
查看次数

标签 统计

java ×2

integer-division ×1

java.util.scanner ×1