簡體   English   中英

找不到nextline / int的行

[英]No line found for nextline/int

我嘗試讓用戶在while循環中輸入string和int中的一些值,但是,當我開始時我沒有找到Line,我盡我所能來解決它​​,但沒有任何對我有用。 我想知道如何讓用戶輸入int和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);使用Scanner in = new Scanner(System.in); 讓我從控制台獲取用戶輸入。 另一個是阻止它覆蓋文本,這是他對這篇文章的最新評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM