This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes syntax is horrible, need help fast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "syntax is horrible, need help fast" Watch "syntax is horrible, need help fast" New topic
Author

syntax is horrible, need help fast

Nick Allen
Ranch Hand

Joined: Sep 25, 2003
Posts: 47
i need to make my program print the length of the first and last words of whatever my end user types in


'I need something good to die for, to make it beautiful to live' -QSA
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
so?
Sit down, think the problem through, make some diagrams or rough sketches of how it should do it, and start coding.


42
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
How about giving us some pseudo code of what you plan to do, Nick.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Nick Allen
Ranch Hand

Joined: Sep 25, 2003
Posts: 47
import java.io.*;
class StringProgram
{
public static void main(String[] Args) throws Exception
{
// Create a link to the keyboard
BufferedReader ty = new BufferedReader(new InputStreamReader(System.in));

// Prompt the user to enter their name
System.out.println("Type in something:");
System.out.print(" > ");

// Read in the user's input
String typein = ty.readLine();

// Print the length of the user's input
System.out.println("Your text is " + typein.length() + " characters long.");

this is how far i've gotten on this, now i need to isolate the first and last words of what gets typed in and print the length of the them, i have tried substringing the typein by the first and last occurence of ' ' but this crashes if a user types a one or two word phrase.
p.s. sorry i'm new at this
Nick Allen
Ranch Hand

Joined: Sep 25, 2003
Posts: 47
what is wrong with this:
String length1 = typein.indexOf(" ");
i thought that this would work but i'm still recording an error on this line
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Here's something I put together but haven't tested. Give it a try.
Angel

[ September 26, 2003: Message edited by: Angel Dobbs-Sciortino ]
Nick Allen
Ranch Hand

Joined: Sep 25, 2003
Posts: 47
thanks for your input angel, i just figured it out another way, but i like that yours talks back if someone doesn't cooperate
i'm having trouble getting the index of the first occurence the number '10' in my program
i tried:

String ten = input.indexOf('10');

and

int ten = input.indexOf('10');
both return an error of 'unclosed character literal'
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
String ten = input.indexOf('10');
This will not work because indexOf returns an integer, not a String.
int ten = input.indexOf('10');
A character can only be one character, and 10 is two characters. Try this:
int ten = input.indexOf("10");
Angel
[ September 26, 2003: Message edited by: Angel Dobbs-Sciortino ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

[ for the first problem...started it then was interupted by a meeting ]
not sure what java version you are using but it is jdk 1.4 then String.split(" ") and take the first and last Strings in the String array result.
Or simply:
use the substring method:

Jamie
[ September 26, 2003: Message edited by: Jamie Robertson ]
Nick Allen
Ranch Hand

Joined: Sep 25, 2003
Posts: 47
thanks for all your help guys
i learned a lot today
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: syntax is horrible, need help fast
 
Similar Threads
Problems starting PetStore cloudscape database on Weblogic 6.1
Part II Am I overlooking something
need help abt 64 encoder/decoder
migrate program from unix to windows, confuse about Path
I think I need this