Ok so I'm trying to create a programmed where the user is asked to enter a word, this word is then stored as a variable, and then its displayed as an array.
我在尝试创建一个程序用户被要求输入一个单词,这个单词被存储为变量,然后它被显示为数组。
This is what I have so far:
这是我目前所拥有的:
package coffeearray;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String drink;
String coffee;
int [] a =new int[6];
Scanner in = new Scanner(System.in);
System.out.println("Please input the word coffee");
drink = in.next();
So basically I need some code so that the word stored is then displayed as variable. For example "Coffee" would then be displayed but each letter on its own line.
基本上我需要一些代码,这样存储的单词就会显示为变量。例如,“咖啡”会被显示出来,但每封信都在自己的线上。
I've searched all over and can't seem to find out how to do this. Thanks in advance.
我找遍了所有的地方,似乎找不到怎么做这件事。提前谢谢。
3 个解决方案
#1
5
Given string str :
给定字符串str:
String str = "someString"; //this is the input from user trough scanner
char[] charArray = str.toCharArray();
Just iterate trough character array. I'm not going to show you this as this can easily be googled. Again if you're really stuck let me know, but you should really know this.
只需迭代槽字符数组。我不会给你们展示这个,因为这很容易被谷歌搜到。如果你真的被困住了,请告诉我,但你真的应该知道。
Update Since you're new to Java. Here is the code per Jeff Hawthorne comment :
更新,因为您是Java新手。以下是杰夫·霍桑的评论:
(for int i=0, i < charArray.getlength(); i++){
System.out.println(charArray[i]);
}
#2
0
You can use String.split("")
您可以使用String.split(" ")
Try this:
试试这个:
String word = "coffee"; //you get this from your scanner
if(word.length()>0)
String [] characterArray = Arrays.copyOfRange(word.split(""), 1, word.length()+1);
#3
0
You can convert the String to a CharSequence:
可以将字符串转换为字符序列:
CharSequence chars = inputString.subSequence(0, inputString.length()-1);
you can then iterate over the sequence, or get individual characters using the charAt() method.
然后可以对序列进行迭代,或者使用charAt()方法获得单个字符。
#1
5
Given string str :
给定字符串str:
String str = "someString"; //this is the input from user trough scanner
char[] charArray = str.toCharArray();
Just iterate trough character array. I'm not going to show you this as this can easily be googled. Again if you're really stuck let me know, but you should really know this.
只需迭代槽字符数组。我不会给你们展示这个,因为这很容易被谷歌搜到。如果你真的被困住了,请告诉我,但你真的应该知道。
Update Since you're new to Java. Here is the code per Jeff Hawthorne comment :
更新,因为您是Java新手。以下是杰夫·霍桑的评论:
(for int i=0, i < charArray.getlength(); i++){
System.out.println(charArray[i]);
}
#2
0
You can use String.split("")
您可以使用String.split(" ")
Try this:
试试这个:
String word = "coffee"; //you get this from your scanner
if(word.length()>0)
String [] characterArray = Arrays.copyOfRange(word.split(""), 1, word.length()+1);
#3
0
You can convert the String to a CharSequence:
可以将字符串转换为字符序列:
CharSequence chars = inputString.subSequence(0, inputString.length()-1);
you can then iterate over the sequence, or get individual characters using the charAt() method.
然后可以对序列进行迭代,或者使用charAt()方法获得单个字符。