简体   繁体   中英

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"

To do that, I searched up the split method 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[].

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.

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.

I didn't learn the split method either, so I'm not supposed to use it to be honest.

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.

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. 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.

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. It describes what the split method does. What you need to do is like the reverse of that. You may use a for loop or a while loop.

*Since you are new to programming,

output += ","

is the same as

output = output + ","

Use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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