Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将多个字符串放入数组_Java_Arrays_String_For Loop_Toupper - Fatal编程技术网

Java 将多个字符串放入数组

Java 将多个字符串放入数组,java,arrays,string,for-loop,toupper,Java,Arrays,String,For Loop,Toupper,我的家庭作业有困难。我记下了基本的逻辑,但我搞砸了。目标是从用户输入的购物清单中生成收据。例如,用户输入: 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 mai

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

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);
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.printf(“输入您想要购买的物品:”;
字符串[]输入=新字符串[keyboard.nextLine()];
keyboard.nextLine();//使用上面的输入
for(int i=0;i
我知道我最终必须添加if语句,因此如果他们键入“!checkout”,列表将结束。但我还没过这关


有任何提示或建议吗?

尝试使用以下代码将完全满足您的要求:

       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());
           }               
        }
扫描仪键盘=新扫描仪(System.in);
List inputItems=new ArrayList();
List printedResults=new ArrayList();
字符串输入=键盘.nextLine();
而(!“!checkout”.equals(输入))
{           
inputItems.add(输入);
输入=键盘.nextLine();
}

对于(int i=0;iYo非常感谢!这非常有帮助,特别是对于将来的参考!!干杯!我很高兴它是有用的。正如其他评论所提到的,指定它是哪种语言是很好的。我有什么方法可以联系你帮助我解决另一个问题吗?嘿,Corey,我的电子邮件是:ali_2004t@yahoo.com,请随时与我联系。嗨,贾罗德罗伯森,请你再检查一遍,这个问题听起来与你提到的问题不一样。标题非常相似,但如果我们仔细看一下描述,那是两种不同的情况。这是一个很好的开始,但我需要它逐行打印水果列表(与输入的方式相同)。此外,数量必须在输出中显示,如橙子x1,我正在修改此代码,以了解如何添加该功能。如果您了解其他方面的信息,请告诉我谢谢!!
       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());
           }               
        }