0
import java.util.Scanner;

public class Assignment6 {
    public static void main(String[] args)
    {

        String projName;
        int projNumber;
        String projLocation;
        double initialFunding;
        double spending;
        double currentBalance;

        Project project = new Project(2.0);
        Scanner keyboard = new Scanner(System.in);

        String userInput; 
        char userChoice;




        do {
            System.out.println("Choice\t\tAction\n "
                    + "------\t\t------\n"
                    + " A\t\tAdd Project\n "
                    + "D\t\tDisplay Project\n "
                    + "Q\t\tQuit\n "
                    + "R\t\tAdd Expenditure\n "
                    + "?\t\tDisplay Help\n\n");

            System.out.println("What action would you like to perform?");
            userInput = keyboard.nextLine().toUpperCase();
            userChoice = userInput.charAt(0);

            switch (userChoice) {

            case 'A':
                System.out.println("Please enter the project information:");

                System.out.println("Enter a project name:");
                project.setName(keyboard.nextLine());

                System.out.println("Enter a project number:");
                project.setNumber(keyboard.nextInt());
                keyboard.nextLine(); // consuming the \n

                System.out.println("Enter a project location:");
                project.setLocation(keyboard.nextLine());

                System.out.println("Enter a project initial funding:");
                initialFunding = keyboard.nextDouble();


                break;
            case 'D':
                System.out.println(project.toString());
                break;
            case 'Q':
                //Quit here just in case.
                break;
            case 'R':
                break;
            case '?':
                break;
            }



        } while (userChoice != 'Q');
    }
}

I do not know why I get an error after I enter the initial funding value.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at Assignment6.main(Assignment6.java:34)

LadyGaga
  • 29
  • 2
  • 3
    You consumed the newline after reading the project number. Why didn't you do the same after the project initial funding? – rgettman Mar 31 '16 at 00:18
  • It probably means that you pressed enter before providing a single letter to console. So input string has length 0 so when you take first letter (at index 0) it gives you an error. – Julian Rubin Mar 31 '16 at 00:20
  • @rgettman The error is thrown by `charAt(0)`. – RaminS Mar 31 '16 at 00:22
  • 1
    @Gendarme Correct. Not consuming the newline after `keyboard.nextDouble` means that the next loop, calling `nextLine`, gets an empty string, causing the exception. – rgettman Mar 31 '16 at 00:23

0 Answers0