找不到针对nextline/int的行 [英] No line found for nextline/int

查看:75
本文介绍了找不到针对nextline/int的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让用户在while循环中以字符串和int形式输入一些值,但是,当我启动时没有找到Line时,我会尽力解决该问题,但对我没有任何帮助.我不知道如何让用户输入int和string的值.

I try to let the user input some values in string and int in while loop, however, when I start I get no Line found I tried my best on my own to solve it but nothing worked for me. I wonder how I let the user input the values for the int and string.

public static void main(String[] args) throws IOException {
    // TODO code application logic here

    int saldo = 10000;
    String anw = "j";
    int cost;


    //create file
    File f = new File("C:\\Users\\ae65255\\Desktop\\file.txt");
    try{


        f.createNewFile();
        System.out.println("23");
                        //frågan
    while(anw.equals("j")){

        Scanner fileIn = new Scanner(f);
        System.out.println("Ange utgiftspost:"); //post
        String post = fileIn.nextLine();
        System.out.println("Ange kostnad:"); //kost
        cost=fileIn.nextInt();
        fileIn.nextLine();
        saldo = +saldo -cost; 
        System.out.println("saldo:" +saldo);
        System.out.println("Vill du mata in fler uppgifter? (j/n) :");
        anw = fileIn.nextLine();
        String fileContent = "---"+post+"---"+cost+"---"+saldo; 
        PrintWriter writer ;
           writer = new PrintWriter(f);
           writer.println(fileContent);


        if (anw.equals("n")) {
            fileIn.close();
            writer.close();
        //System.out.println("---"+post+"---"+cost+"---"+saldo);                

        }
    } //frågan slut


    }
      catch(Exception e){
  System.err.println(e.getMessage());
}//frågan
        //System.out.println("---"+post+"---"+cost+"---"+saldo);                
    System.out.println("File path" +f.getPath());

    }


}

推荐答案

@AKSW注释解决了我的两个答案.使用Scanner in = new Scanner(System.in);将使我从控制台获取用户输入.另一种是阻止它覆盖他最近的对这篇文章发表评论.

@AKSW comment solved my two answers. Using Scanner in = new Scanner(System.in); would let me take the user input from the console. The other was to stop it overwrite the text it which was his latest comment on this post.

这篇关于找不到针对nextline/int的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆