简体   繁体   English

分割用户输入的每个字符串,并带有逗号错误

[英]Splitting every String the user enters with a comma error

I am supposed to split every string the user enters with a comma. 我应该用逗号分割用户输入的每个字符串。 For example, if the user enters in "Rabbit", I am supposed to print it out as "R, a, b, b, i, t" 例如,如果用户输入“兔子”,则应该将其打印为“ R,a,b,b,i,t”

To do that, I searched up the split method String.split(); 为此,我搜索了拆分方法String.split();。

The code I wrote is as follows 我写的代码如下

import java.util.*;

class Split{

    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a String");
        String str= input.nextLine();
        printLetters(str);
    }

    public static String[] printLetters (String a){
        return a.split(",");
    }
}

Actually I am supposed to write the code using a method that accepts Strings, but when I wrote the String.split() , it said I have to use String [], so I just changed it to String[]. 实际上,我应该使用接受Strings的方法来编写代码,但是当我编写String.split() ,它说我必须使用String [],因此我只是将其更改为String []。

But that code didn't print anything. 但是该代码没有输出任何内容。 I want to know why that is so, and also want to know how to accomplish this task using public static String. 我想知道为什么会这样,也想知道如何使用公共静态String完成此任务。

I know there are lots of posts related to it, but I couldn't find anything that matches the requirement, which is using public static String. 我知道有很多与此相关的文章,但是我找不到符合要求的任何东西,这是使用公共静态String的。

I didn't learn the split method either, so I'm not supposed to use it to be honest. 我也没有学习split方法,所以说实话我不应该使用它。

This is my first year taking computer science, and the only things I know are for loops, if else, nested loops, etc. 这是我参加计算机科学专业的第一年,我所知道的唯一内容是循环,如果还有循环嵌套等等。

It would be preferable not to use the split method, but it's ok to use it because I'm curious about it too. 最好不要使用split方法,但是可以使用它,因为我也对此很好奇。

Thank you in advance :) 先感谢您 :)

From what I can see, you are trying to take an input string and return a new string with inserted commas after every character. 据我所知,您正在尝试获取一个输入字符串,并在每个字符后返回一个带有插入逗号的新字符串。

String.split() does something quite different. String.split()所做的事情完全不同。 For example, if I had "R,a,b,b,i,t", I could use String.split() with the delimiter , to get ["R", "a", "b", "b", "i", "t"] back. 例如,如果我有“ R,a,b,b,i,t”,则可以将String.split()与分隔符一起使用,以获取[“ R”,“ a”,“ b”,“ b” , “回来了。

A simple solution to your problem would be to iterate through the input string character by character and after each character except for the last one, add a comma to the return string. 解决您的问题的一种简单方法是逐个字符地遍历输入字符串,然后在除最后一个字符之外的每个字符之后,在返回字符串中添加逗号。

Since your assignment is to actually do this yourself, I won't be posting a code solution. 由于您的工作实际上是自己完成,因此我不会发布代码解决方案。 However, I hope my explanation cleared up your misconceptions and is enough for you to write a solution. 但是,我希望我的解释消除了您的误解,足以让您编写解决方案。

public static void printLetters (String a){
    String output = "";
    for (int i=0; i<a.length(); i++)
    {
      output += a.charAt(i);
      if (i != a.length()-1)
      output += ",";
    }
    System.out.println(output);
}

See @Andrew Fan's answer. 请参阅@Andrew Fan的答案。 It describes what the split method does. 它描述了split方法的作用。 What you need to do is like the reverse of that. 您需要执行的操作与此相反。 You may use a for loop or a while loop. 您可以使用for循环或while循环。

*Since you are new to programming, *由于您是编程新手,

output += ","

is the same as 是相同的

output = output + ","

Use String.join() . 使用String.join()

import java.util.*;

    class Split{

        public static void main(String [] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a String");
            String str= input.nextLine();
            String word = printLetters(str);
            System.out.println(word);
        }

        public static String printLetters (String a){
            return String.join(", ", a.split(""));
        }
    }

在此处输入图片说明

See String.join() and String.split() for more info. 有关更多信息,请参见String.join()String.split()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM