No line found for nextline/int

2019-08-21 17:18发布

问题:

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());

    }


}

回答1:

@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.