Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using arrays with Char

 
James Brennan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way.
You'll need to read the word into (in this e.g.) str
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic