英文:
Splitting a String by two things?
问题
import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a;
System.out.println("Type in your pinochle deals: ");
a = sc.nextLine();
sc.close();
String[] deals = a.split(",");
String concatenatedDeals = String.join("", deals);
String[] finalDeals = concatenatedDeals.split("");
}
}
import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a;
System.out.println("Type in your pinochle deals: ");
a = sc.nextLine();
sc.close();
String[] deals = a.split(",");
String concatenatedDeals = String.join("", deals);
String[] finalDeals = concatenatedDeals.split("");
// At this point, finalDeals will contain the desired output.
// You can print it to verify.
System.out.print("[");
for (int i = 0; i < finalDeals.length; i++) {
System.out.print("\"" + finalDeals[i] + "\"");
if (i < finalDeals.length - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}
英文:
I have the following code:
import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
String a;
System.out.println("Type in your pinochle deals: ");
a=sc.nextLine();
sc.close();
String[] deals=a.split("");
}
}
I need to split the String I named "a"
into a 16 spaced array. But the problem with the splitting is that the input is something like this: ATKQQJ,AKQQ,KQQJN,A
. I need to split this into 16 parts and save it to an array I named "deals."
I've tried String[] deals=a.split("" && ",");
but apparently that isn't valid. I've also tried to split String a into 2 separate arrays and then put them together, but I realized I didn't know how. I want the output to be ["A","T","K","Q","Q","J","A","K","Q","Q","K","Q","Q","J","N","A"]
when the input is: ATKQQJ,AKQQ,KQQJN,A
. How should I accomplish this?
答案1
得分: 1
// 将其拆分为单个字符
System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").toCharArray()));
// 将其拆分为单个字符的字符串
System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").split("")));
英文:
//Split it out into individual characters
System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").toCharArray()));
//Split it into strings of a single character
System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").split("")));
答案2
得分: 1
import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
String a, b = "";
System.out.println("Type in your pinochle deals: ");
a = sc.nextLine();
sc.close();
String[] temp = a.split(",");
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[i].length(); j++) {
b += temp[i].charAt(j);
}
}
char[] deals = new char[b.length()];
for (int i = 0; i < b.length(); i++) {
deals[i] = b.charAt(i);
}
}
}
英文:
Try this code
import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
String a,b="";
System.out.println("Type in your pinochle deals: ");
a = sc.nextLine();
sc.close();
String[] temp = a.split(",");
for (int i=0; i<temp.length; i++){
for (int j=0; j<temp[i].length(); j++){
b+=temp[i].charAt(j);
}
}
char[] deals=new char[b.length()];
for (int i=0; i<b.length(); i++){
deals[i]=b.charAt(i);
}
}
}
答案3
得分: 0
这里是您要的内容:
String a = "ATKQQJ,AKQQ,KQQJN";
// 将字符串按逗号分隔成部分
String[] parts = a.split(",");
// 将这些部分合并为一个单独的字符串
String whole = String.join("", parts);
// 最后,将其分割为单个字母
String[] letters = whole.split("");
也可以通过从 a
中删除逗号来生成 whole
。
英文:
Here you go:
String a = "ATKQQJ,AKQQ,KQQJN,A";
// Split the string into comma-separated parts
String[] parts = a.split(",");
//Join those parts into a single string
String whole = String.join("",parts);
//Finally, split it up into individual letters
String[] letters = whole.split("");
Could have also generated whole
by removing the commas from a
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论