File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

syntax is horrible, need help fast

 
Nick Allen
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to make my program print the length of the first and last words of whatever my end user types in
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so?
Sit down, think the problem through, make some diagrams or rough sketches of how it should do it, and start coding.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about giving us some pseudo code of what you plan to do, Nick.
 
Nick Allen
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ 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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all your help guys
i learned a lot today
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic