Jonathan Russell
Jonathan Russell

Reputation: 37

Java scanner not returning multiple word variable

This section of my code is not working as I need it to. I need to Select from the menu either A, B, or X and then input a small sentence for the description (multiple) a figure for the cost. I can input all the data perfectly and it returns to the menu (only exiting once inputing X). But the return string for the variable damageDesc is only grabbing the first word prior to the space. I've tried this all night and still can't get it to return all words in the string. Here's the code if anyone can help. Thanks!

do {
     System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
        "Menu");
     System.out.println("-------------------------------------------------"+
        "---");

     System.out.println();

     System.out.println("A - Record Damage Repair Details");
     System.out.println("B - Record Traffic Infringement Details");
     System.out.println("X - Exit");

     System.out.println();

     System.out.print("Enter your selection (A/B/X):");
     menuOption = console.next().charAt(0);

     switch (menuOption) {

        case 'A':
        case 'a':
           System.out.print("Enter description of damage repair: ");
           damageDesc = console.next();
           console.nextLine();
           System.out.print("Enter repair cost: ");
           damageCost= console.nextDouble();
           //Place damage description and cost on seperate lines 
           damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, damageCost);

           //Also could have used the below
           //damageDescFinal = (damageDescFinal+"- "+damageDesc+
           //   " ($"+damageCost+")"+"\n");

           //All damage costs added together 
           damageCostFinal = damageCostFinal + damageCost;
           System.out.println();
        break;


        case 'B':
        case 'b':
           System.out.print("Enter details of traffic infringement: ");
           fineDesc = console.next();
           console.nextLine();
           System.out.print("Enter traffic fine amount: ");
           fineCost = console.nextDouble();
           //Set fine description and cost on seperate lines
           fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, fineCost);

           //Also could have used the below
           //fineDescFinal = fineDescFinal+"- "+fineDesc+
           //   " ($"+fineCost+")"+"\n";
           //All fine costs added together 
           fineCostFinal = fineCostFinal + fineCost;
           System.out.println();
           break;

        case 'X':
        case 'x':
           //Exit the damage and fine menu
           System.out.print("Exiting data entry menu..."+"\n");
           System.out.println();
           break;

        default:
           //Error handling for invalid input
           System.out.print("***ERROR*** Invalid Selection!"+"\n");
           System.out.println();
           break;

     }
  } while (menuOption != 'X' && menuOption != 'x');

Upvotes: 0

Views: 226

Answers (3)

Kain
Kain

Reputation: 1

why not in this case use .ToLowerCase or .ToUperCase? this way you can get a cleaner code

do {
        System.out.println("Damage Repair / Traffic Infringement Data Entry "+
                "Menu");
        System.out.println("-------------------------------------------------"+
                "---");
        System.out.println();
        System.out.println("A - Record Damage Repair Details");
        System.out.println("B - Record Traffic Infringement Details");
        System.out.println("X - Exit");
        System.out.println();
        System.out.print("Enter your selection (A/B/X):");
        Scanner console = new Scanner(System.in);
        menuOption = console.next().toLowerCase().charAt(0);

        //Switch to allow either upper or lower case menuOption
        switch (menuOption) {


            case 'a':
                System.out.print("Enter description of damage repair: ");
                //Clear scanner
                console.nextLine();
                //Input damage description
                damageDesc = console.nextLine();
                System.out.print("Enter repair cost: ");
                //Input damage cost
                damageCost= console.nextLine();
                //Convert string fineCost back to a double
                //This was input as a String as scanner produces an error
                double doubleDamageCost = Double.parseDouble(damageCost);
                //Set damage description and cost on seperate lines for each input
                damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
                //Concatenate all fine costs for a final value 
                damageCostFinal = damageCostFinal + doubleDamageCost;
                System.out.println();
                break;

            case 'b':
                System.out.print("Enter details of traffic infringement: ");
                //Clear scanner
                console.nextLine();
                //Input fine description
                fineDesc = console.nextLine();
                System.out.print("Enter traffic fine amount: ");
                //Input fine cost
                fineCost = console.nextLine();
                //Convert string fineCost back to a double
                //This was input as a String as scanner produces an error
                double doubleFineCost = Double.parseDouble(fineCost);
                //Set fine description and cost on seperate lines for each input
                fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc,              doubleFineCost);
                //Concatenate all fine costs for a final value
                fineCostFinal = fineCostFinal + doubleFineCost;
                System.out.println();
                break;

            case 'x':
                //Exit the damage and fine menu
                System.out.print("Exiting data entry menu..."+"\n");
                System.out.println();
                break;

            default:
                //Error handling for invalid input
                System.out.print("***ERROR*** Invalid Selection!"+"\n");
                System.out.println();
                break;

        }
        //Exits menu loop and allow code to continue
    } while (menuOption != 'x');

but you should do more classes like

Public void menu(){

 System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
    "Menu");
 System.out.println("-------------------------------------------------"+
    "---");
 System.out.println();
 System.out.println("A - Record Damage Repair Details");
 System.out.println("B - Record Traffic Infringement Details");
 System.out.println("X - Exit");
 System.out.println();
 System.out.print("Enter your selection (A/B/X):");

}

and so on, to get a more organized and clean code. segementation is your best friend.

Upvotes: 0

Jonathan Russell
Jonathan Russell

Reputation: 37

Fixed this myself by changing the Double values to strings and then converted them back to a double after the inputs were finished. This solved the scanner problems I was having. Here's the final code that worked:

  do {
     System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
        "Menu");
     System.out.println("-------------------------------------------------"+
        "---");
     System.out.println();
     System.out.println("A - Record Damage Repair Details");
     System.out.println("B - Record Traffic Infringement Details");
     System.out.println("X - Exit");
     System.out.println();
     System.out.print("Enter your selection (A/B/X):");
     menuOption = console.next().charAt(0);

     //Switch to allow either upper or lower case menuOption
     switch (menuOption) {

        case 'A':
        case 'a':
           System.out.print("Enter description of damage repair: ");
           //Clear scanner
           console.nextLine();
           //Input damage description
           damageDesc = console.nextLine();
           System.out.print("Enter repair cost: ");
           //Input damage cost
           damageCost= console.nextLine();
           //Convert string fineCost back to a double
           //This was input as a String as scanner produces an error
           double doubleDamageCost = Double.parseDouble(damageCost);
           //Set damage description and cost on seperate lines for each input
           damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
           //Concatenate all fine costs for a final value 
           damageCostFinal = damageCostFinal + doubleDamageCost;
           System.out.println();
           break;

        case 'B':
        case 'b':
           System.out.print("Enter details of traffic infringement: ");
           //Clear scanner
           console.nextLine();
           //Input fine description
           fineDesc = console.nextLine();
           System.out.print("Enter traffic fine amount: ");
           //Input fine cost
           fineCost = console.nextLine();
           //Convert string fineCost back to a double
           //This was input as a String as scanner produces an error
           double doubleFineCost = Double.parseDouble(fineCost);
           //Set fine description and cost on seperate lines for each input
           fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, doubleFineCost);
           //Concatenate all fine costs for a final value
           fineCostFinal = fineCostFinal + doubleFineCost;
           System.out.println();
           break;

        case 'X':
        case 'x':
           //Exit the damage and fine menu
           System.out.print("Exiting data entry menu..."+"\n");
           System.out.println();
           break;

        default:
           //Error handling for invalid input
           System.out.print("***ERROR*** Invalid Selection!"+"\n");
           System.out.println();
           break;

     }
  //Exits menu loop and allow code to continue
  } while (menuOption != 'X' && menuOption != 'x');

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Perhaps, you need to use nextLine() for reading the entire line instead of next

Upvotes: 1

Related Questions