Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Very basic program but its making me crazy

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very basic program but its making me crazy

    First post , hello

    I'm new to java and I'm trying to get this basic program working to find the shortest word in a string.
    for some reason when it runs it will always declare the last word entered in the string as being the shortest word , regardless how many letters are in it or if its a word or an integer entered.

    Can anyone see what im doing wrong?

                    String  sentence; 
    		String  shortestword;
    		String  word;
    		int       spacePos;
     
    		shortestword = "";
     
    		System.out.print("Please enter a sentence: ");
    		sentence = getString();
     
    			do{
     
     
    				spacePos = sentence.indexOf(" ");
     
     
    				if (spacePos == -1)
     
    			{
    					word = sentence;
     
    			} 
     
    				else
     
    				{
     
    				word = sentence.substring(0,spacePos);
    				sentence = sentence.substring(spacePos +1);
     
                    }
     
     
    				if(word.length() > shortestword.length())
     
     
    			{
    				   shortestword = word;
    			}
     
     
    			   }
     
    			   while (spacePos != -1);
     
    			   System.out.println("The shortest word is " + word)
    Last edited by craig carl; February 2nd, 2011 at 01:12 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Very basic program but its making me crazy

    When posting code, make sure you use the code or highlight tags to preserve formatting. Also, make sure it's in the form of an SSCCE (that's a link).

    But have you tried putting in some print statements to figure out what's going on? Or better yet, have you tried running through this with a debugger?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic program but its making me crazy

    Quote Originally Posted by KevinWorkman View Post
    When posting code, make sure you use the code or highlight tags to preserve formatting. Also, make sure it's in the form of an SSCCE (that's a link).

    But have you tried putting in some print statements to figure out what's going on? Or better yet, have you tried running through this with a debugger?
    hi Kevin

    added the code tags

    It compiles and runs fine in j-creator, it's just outputting the wrong answer and I cant for the life of me figure out why , this might sound naive but whats a debugger?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Very basic program but its making me crazy

    From my point of view, Debugger might be over the top for a beginner but that's not here or there.
    Problems in your program are very small, and once you spot them, it will take seconds to adjust.
    I recommend checking out:
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic program but its making me crazy

    thanks for the help ,ill go back in on it now , you would laugh if you knew how long i have been plugging away at this and i just know its something very simple

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Very basic program but its making me crazy

    Deeply sorry, Posted advice without checking it on my own System! Although suggestions I pointed towards were needed, It was not a clear-cut (Quote: Few second Job) like I said.

    Look into the split method of the String class:
    String (Java 2 Platform SE v1.4.2)

    By splitting up your sentence, and storing it into a String Array, you can easily go through the array comparing lengths with only a fraction of your code.

    The parameter of split() is something you consider to be a good checkpoint to split the string, such as : or a whitespace.

    Click here for details on for loops
    Click here for an example of String.split() in action.

    Again, very sorry for not checking my post before saying It was a 2 second job for you
    Last edited by newbie; February 2nd, 2011 at 02:26 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. The Following User Says Thank You to newbie For This Useful Post:

    c.P.u1 (February 12th, 2011)

Similar Threads

  1. Not sure what to do next (Basic java program)
    By desi22601 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2010, 09:05 AM
  2. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  3. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 10:36 PM
  4. Basic Java Program Help
    By roaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2009, 10:28 PM
  5. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM