字符串输出仅返回一个单词 [英] String output only returning one word

查看:64
本文介绍了字符串输出仅返回一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序运行得很好,但是由于某些原因,当我要求输入名称时,它只会在响应中保存第一个单词.例如,如果我在稍后使用它输入输出时输入"Jon Snow",它将仅显示"Jon".

This program runs just fine, but for some reason when I ask for an input for name it will only save the first word in the response. For example if I enter "Jon Snow" when I use it later for output it will only show "Jon".

import java.util.Scanner;

public class help 
{
public static void main (String[] args)
{
    Scanner input = new Scanner(System.in); //object for user input

    //constant
    final double tax = 0.08; //Sales tax

    //variables
    int choice;                 //menu selection/case switch
    String name;                //customer's name   
    String address;             //customer's address
    String email;               //customer's email address
    String item;                //item purchased
    double itemsPurchased;      //number of items purchased
    double itemPrice;           //price of item purchased
    double total;               //total for purchase
    double grandTotal;          //total + tax
    double taxAmount;           //tax of the purchase

    //Menu
    System.out.println("1. Enter Customer Information");
    System.out.println("2. Display Total Bill");
    System.out.println("3. Quit\n");                            
    System.out.print("Enter 1, 2, or 3 to make your selection: ");
    choice = input.nextInt();

    switch(choice){

    //Customer info

    case 1:

        //inputting info
        System.out.println("\nPlease enter the customers information.");

        //name
        System.out.print("Name: "); 
        name = input.next();
        input.nextLine();                                           

        //address
        System.out.print("Address: ");
        address = input.nextLine();

        //email
        System.out.print("Email Adress: ");
        email = input.next();
        input.nextLine();                                           

        //reading info back to user
        System.out.println("\nThe customer has successfully been added to our list with the following information: ");
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Emal: " + email);
        break;                      

    //Customer receipt  

    case 2:

        //name
        System.out.println("");                                     
        System.out.print("Enter customer's name: ");
        name = input.next();
        input.nextLine();                                           

        //name of item purchased
        System.out.print("Enter item purchased: ");
        item = input.next();
        input.nextLine();                                           

        //number of items purchased
        System.out.print("Number of items purchased: ");
        itemsPurchased = input.nextDouble();
        input.nextLine();                                           

        //price of item
        System.out.print("Price of item: ");
        itemPrice = input.nextDouble();
        input.nextLine();                                           

        //defining taxAmount, total, and grandTotal
        total = (itemPrice * itemsPurchased);
        taxAmount = (total*tax);
        grandTotal = total + taxAmount;

        //bill

        System.out.println("");                                                                         
        System.out.println(name);
        System.out.println("");                                                                         
        System.out.printf("%-20s %-15s %-15s\n", "Product Purchased", "Quantity", "Total Cost");            
        System.out.println("");
        System.out.printf("%-20s %-15s %-15s\n", item, itemsPurchased, total);
        System.out.println("");                                                                         
        System.out.printf("%-20s %-15s %-15s\n", "Tax(@8%):", "", taxAmount);
        System.out.printf("%-20s %-15s %-15s\n", "Total Cost:", "", grandTotal);

        break;                      

    //Quit

    case 3:

        System.out.println("End Program");
        break; //void

    //Default statement

    default:
        System.out.print("Invalid value entered.");

    }//end case

}//end main

}//end class

推荐答案

name = input.nextLine()

您不会阅读整行,而只会阅读第一个单词.

You are not reading the whole line but only the first word.

对于 Scanner 类, next()函数读取下一个标记化的输入,而 nextLine()检索整行,直到回车为止返回( \ n ).

For the Scanner class, the next() function reads the next tokenized input while nextLine() retrieves the whole line until the carriage return (\n).

Eg. "happy days again"
next() // "happy"
next() // "days"
next() // "again"

or,

nextLine() // "happy days again"

尝试下面的代码

input.nextLine(); // IMP: To get the carriage return after choice is typed
//name
System.out.println("");                                     
System.out.print("Enter customer's name: ");
name = input.nextLine();                                           

//name of item purchased
System.out.print("Enter item purchased: ");
item = input.nextLine(); 

这篇关于字符串输出仅返回一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆