mercredi 25 novembre 2020

Any suggestions or solutions regarding this matter? It ceased looping at its second loop [closed]

package toto.com;

import java.util.Scanner;

public class projectory {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);


        double necessitatedBudget = Double.parseDouble(scanner.nextLine());
        double currentBudget = Double.parseDouble(scanner.nextLine());
      //Explained with brevity, it ceased looping immediately after it had begun its second loop , more specifically at the second line in the loop 

     **//The input in this case should 
      //2000
      //1000
      //spend
      //1200
      //save
      //2000
       
    // the output is at the bottom.  
     
        int receptacle = 0;
        int leftMoney = 0;
        int days = 0;
        boolean test = false;
        while (necessitatedBudget >= leftMoney) {
            String command = scanner.nextLine();
            int spent = Integer.parseInt(scanner.nextLine());///it stops exactly here after it has already done 1 loop. The error is at the bottom.
            if (command.equals("spend")) {
                double devalued = currentBudget - spent;
                if (devalued > 0) {
                    leftMoney += devalued;
                } else {
                    leftMoney = 0;
                }
                receptacle++;
                if (receptacle >= 5) {
                    test = true;
                    break;
                }

            }else if (command.equals("save")) {
                leftMoney += spent;

            }

            days++;
        }

        String output = "";

        if (test) {
            output = String.format("You can't save the money. %n" +
                    "%d", receptacle);
        } else if (leftMoney == necessitatedBudget) {
            output = String.format("You saved the money for %d days.", days);
        }

        System.out.println(output);
    }

}
   ***desired output - You saved the money for 2 days.***

Tthe error is the following

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:662) at java.base/java.lang.Integer.parseInt(Integer.java:770) at toto.com.projectory.main(projectory.java:22)

Aucun commentaire:

Enregistrer un commentaire