PDA

View Full Version : String help.


Johnny Redburn
06-02-2004, 11:38 AM
I have failed horribly in attempt to create a string that will read input from the keyboard (asglazxcbwedjsn etc)
Can someone please offer some advice as to how to create one?
-thanks


public static String userinput()
{
char[] text = new char[20] {};
System.out.println("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°);
System.out.println(" Please enter a series of character values from the keyboard: ");
data = (String) System.in.read();
System.out.println("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°);
String lower = data.toLowerCase();
return(data);
}

shmoove
06-02-2004, 11:45 AM
What errors are you getting?

Here's what I have found wrong:

char[] text = new char[20] {};

Declared text but never used it (and what's with the empty block?).

data = (String) System.in.read();

Unless data is a global variable you should declare it.

String lower = data.toLowerCase();
return(data);

You are not using lower...

shmoove

Johnny Redburn
06-02-2004, 02:32 PM
Thanks for replys :)

"Declared text but never used it (and what's with the empty block?)."

Havent really made a string before, i didnt know what to put in the block, im trying to make it so that the user can enter some letters (or invalid keys) and they are saved as a string and sent to main (so that i can then check it against an alphabet array thats in another method. etc)

Spookster
06-02-2004, 03:41 PM
Let's keep this in one topic. You now have several threads on this:

http://www.codingforums.com/showthread.php?t=39152
http://www.codingforums.com/showthread.php?t=39550

You may want to make yourself familiar with the InputStreamReader and BufferedReader classes in conjunction with the standard input. Are you suppose to be working with instantiable classes on this homework assignment or are you suppose to just make several methods under one main class? Instantiable classes would be the best way as to take advantage of Java's object oriented design capabilities.

Johnny Redburn
06-02-2004, 04:31 PM
So far, we have really only made modular programs that have a bunch of methods and a seperate main function.

Im pretty much ok to do other parts of this program (i've already used Bufferedreader/Filereader etc in a different program to read input from a file)
but i just really needed to know how to make "string" that will accept user input(letters/invalid characters), so i can save it as a string and send it to main.
(is it possible to make an empty string so the user can enter an amount of characters?)

Sorry about the other threads :rolleyes:

Spookster
06-02-2004, 08:31 PM
The best way of reading in a line of input from the user in my experience has been to use the BufferedReader class with the InputStreamReader class piping in the standard input. This allows you to read in an entire line of input from the user instead of having to do one character at a time. It can still be used to do one character at a time but this allows for more flexibility.


String myString;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
myString = stdin.readLine();
System.out.println("The string you entered was " + myString);


Does that make it bit clearer?