【问题标题】:Getting multiple Strings into an Array [duplicate]将多个字符串放入一个数组中[重复]
【发布时间】:2016-11-11 05:54:32
【问题描述】:

我的作业有困难。我有基本的逻辑,但我搞砸了。目标是从用户输入的购物清单中制作收据。例如,用户输入:

Apples

OraNgeS // also it's not case sensitive

Oranges

Bananas

!checkout //this is to indicate the list is over

输出:

Apples x1

Oranges x2

Bananas x1

我被困住了。到目前为止我的代码:

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

    System.out.printf("Enter the items you wish to buy:"); 
    String[] input = new String [keyboard.nextLine()];
    keyboard.nextLine(); //consuming the <enter> from input above

    for (int i = 0; i < input.length; i++) {
        input[i] = keyboard.nextLine();
    }

    System.out.printf("\nYour input:\n");
    for (String s : input) {
        System.out.println(s);
    }
}

我知道我最终必须添加 if 语句,所以如果他们输入“!checkout”,它将结束列表。但我现在还过不去。

有什么提示或建议吗?

【问题讨论】:

    标签: java arrays string for-loop toupper


    【解决方案1】:

    尝试使用ArrayList

    Array 需要先静态初始化才能使用它,而 ArrayList 会在你添加值时自动扩展。您可以使用 ArrayList 而无需在其上初始化范围。这是示例:


    List<String> fruitList = new ArrayList<>();
        Scanner keyboard = null;
        Boolean isNotDone = true;
        System.out.println("Press 'Q' if you want to print out.");
        while(isNotDone) {
            keyboard = new Scanner(System.in);
            System.out.print("Input Fruits: ");
            String temp = keyboard.nextLine();
            if(temp.equalsIgnoreCase("q")) {
                isNotDone = false;
            } else {
                fruitList.add(temp);
            }
        }
        System.out.println(fruitList);
    

    【讨论】:

    • 这是一个很好的开始,但我需要它逐行打印出水果清单(与输入方式相同)。此外,数量必须在输出中显示,例如 Oranges x1,我现在正在修改此代码以查看如何添加该功能。让我知道你是否知道其他明智的方式谢谢!
    【解决方案2】:

    以下代码将完全符合您的要求:

           Scanner keyboard = new Scanner(System.in);
           List<String> inputItems = new ArrayList<String>();       
           List<String> printedResults = new ArrayList<String>();
           String input = keyboard.nextLine();       
    
           while(!"!checkout".equals(input)) 
           {           
               inputItems.add(input);           
               input = keyboard.nextLine();
           }
    
           for(int i=0; i<inputItems.size();i++)
           {
               Integer thisItemCount = 0;
               String currentItem = inputItems.get(i);
    
               for(int j=0; j<inputItems.size();j++)
               {
                    if(inputItems.get(j).toLowerCase().equals(currentItem.toLowerCase()))    
                        thisItemCount++;
               }
    
               if(!printedResults.contains(currentItem.toLowerCase()))
               {
                   System.out.println(currentItem.toLowerCase() + " x" + thisItemCount);
                   printedResults.add(currentItem.toLowerCase());
               }               
            }
    

    【讨论】:

    • 哟,非常感谢!这对将来的参考很有帮助!
    • 干杯!我很高兴它很有用。正如其他评论提到的那样,最好指定它是哪种语言。
    • 有什么方法可以联系您帮助我解决其他问题吗?
    • 嗨,Corey,我的邮箱是:ali_2004t@yahoo.com,欢迎联系我。
    • 您好 Jarrod Roberson,请您再检查一次,这个问题听起来与您提到的那个问题并不重复。标题非常相似,但如果我们仔细阅读描述,那么这是两种不同的场景。
    猜你喜欢
    • 2013-03-04
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2019-03-22
    • 2016-07-28
    相关资源
    最近更新 更多