File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Using arrays with Char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using arrays with Char" Watch "Using arrays with Char" New topic

Using arrays with Char

James Brennan

Joined: Mar 07, 2004
Posts: 1
Hi guys.
Im really new to Java and im having a bit of trouble with arrays. I need to input a 3 letter word into an array of 3 elements. Each letter will go into a seperate element. I have got the array to initialise I think? How do I get the program to take the letters inputted and place them in the array elements?
Here is my code so far...
import TerminalIO.*;
public class readinword {
KeyboardReader reader = new KeyboardReader();
ScreenWriter writer = new ScreenWriter();

char[] guessWord = new char[3]; // creates Array of 3 characters

char letter1;
char letter2;
char letter3;
public void readinword(){
writer.print ("Enter the first character of the word to be guessed:");
letter1[0] = reader.readchar();

Im geussing the problem will lie in the final part but im not so sure yet. Any help much appreciated.

<a href="" target="_blank" rel="nofollow"></a>
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
I'm guessing that KeyboardReader and ScreenWriter are classes provided by a tutor or a book to simplify input and output.
You've declared a char array which is good, but then went on to declare three separate char variables which look superflous.
To read a char directly into an element of an array you will use something like:
guessWord[0] = reader.readchar();
That's for the first char. To read in three char's you'll be needing a for loop, something like
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Here's one way.
You'll need to read the word into (in this e.g.) str
I agree. Here's the link:
subject: Using arrays with Char
jQuery in Action, 3rd edition