Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 4:01 AM   #1
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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...
tumbleTetris is offline   Reply With Quote
Old May 25th, 2006, 4:23 AM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
Mjordan2nd is offline   Reply With Quote
Old May 25th, 2006, 4:26 AM   #3
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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?
tumbleTetris is offline   Reply With Quote
Old May 25th, 2006, 4:32 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
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.
Jimbo is offline   Reply With Quote
Old May 25th, 2006, 4:39 AM   #5
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
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.*.
tumbleTetris is offline   Reply With Quote
Old May 25th, 2006, 8:16 AM   #6
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
Because it's main method throws an IOException. IOException is in the package java.io and it's used for exception handling.
Toro is offline   Reply With Quote
Old May 25th, 2006, 10:18 AM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC