![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
Reading a string/sentence from the keyboard.
Just would like two things clarified.
First, is a string the same as a sentance in java? Secondly, how can I get the user to input a string/sentance and store it in a variable? Yes, its an easy question, still getting a hang on the basics... |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
A String is just an array of characters. It can be a sentence, but it's by no means limited to only sentences. To get input (I haven't compiled, so there could be possible syntax errors)
import java.io.*;
import java.util.*;
public class Example
{
public static void main(String [] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
String s = keyboard.nextLine(); // this line waits for input from the user and stores it in s
}
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower Last edited by Mjordan2nd; May 25th, 2006 at 10:18 AM. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
umm, when I print 's' I only get the first word of the sentance.
Am I supposed to create an array of strings to get a sentence from the user? |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Or use the .nextLine() method. That will read in until a newline character. Theres also a .next() which takes a Pattern as a parameter, but I'm not entirely sure how patterns work in Java. Check the documentation for more info on those.
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
yup, .nextLint() done the trick, thanks
![]() Just one more question, why does Mjordan2nd have import java.io.* in the sourcecode? I'm compiling and running my little class just fine without the import.java.io.*. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
Because it's main method throws an IOException. IOException is in the package java.io and it's used for exception handling.
|
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Oh god, sorry. I meant nextLine(). 3:00 at night, I wasn't thinking. Anyway, I corrected my mistake. next() will only read the first word. Sorry about that.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|