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.

share|improve this question
1  
Unfortunately we tend not to just write the code for you - show us what you have so far. – Westie Mar 4 at 13:07

3 Answers

Given string 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 :

(for int i=0, i < charArray.getlength(); i++){
   System.out.println(charArray[i]);
}
share|improve this answer
I'm quite new to Java. I'm assuming I would still need to use the "System.out.println" and then have the code that then breaks up the word into single letters. – user2131803 Mar 4 at 13:20
ant's code DOES break up the words into single letters. as a Java programmer get used to referring back to the API frequently docs.oracle.com/javase/6/docs/api to output it it would be a for loop like (for int i=0, i < charArray.getlength(); i++) System.out.println(charArray[i]); – Jeff Hawthorne Mar 4 at 14:21

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.

share|improve this answer

You can use 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);
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.