Raised This Month: $32 Target: $400
 8% 

Java help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jingojang
Senior Member
Join Date: Feb 2010
Location: The Moon
Old 12-12-2013 , 13:17   Java help
Reply With Quote #1

Hello,

I'm kinda new to Java but I like it and it is a lot of fun. I got some trouble finding out how to print out the first and last word in an entered sentence.

Say you have a JOptionPane.showInputDialog coming up for the user, the user enters a sentence of a minimum of 2 words. When he hits "Ok" I want the first word and last word to be displayed in the same JOptionPane.showMessageDialog.

Ex sentence. Hello world
First word: Hello
Last word: world

I was really trying on my own but did not get that far, I was able to use this on one set String. Here is my code, please help me get so I can type my own sentence.

PHP Code:
package blabla;

import javax.swing.JOptionPane;

public class 
blabla {
    public static 
void main(String[] args)
    {
        
String s "Able was I ere I saw Elba";
        
String firstWord s.substring(0s.indexOf(' '));
        
JOptionPane.showMessageDialog(null"You have entered a total of "
                            
s.length()
                                + 
" characters\n"
                                    
"First word: "
                                        
firstWord
                                            
"\nLast word: "
                                                
s.substring (s.lastIndexOf(' '), s.length()));
    }

__________________
jingojang is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 12-12-2013 , 14:23   Re: Java help
Reply With Quote #2

You need to replace the existing string with a call to JOptionPane.showInputDialog, which returns the input as a String.

Something like
PHP Code:
String s JOptionPane.showInputDialog("Enter a sentence"); 
There are also more complicated forms of showInputDialog that allow you to specify a dialog title, message type, etc...
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 12-12-2013 at 14:24.
Powerlord is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 12-14-2013 , 11:50   Re: Java help
Reply With Quote #3

Look up the String split() function: it splits a string into an array of smaller strings, based on a certain delimiter (basically where you want to separate the strings from). You can make it so that it splits it based on a space, so you will get an array of words, in your case. Then take the 0 index and the length-1 index of that array, and those will give you the first and last words entered.
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
jingojang
Senior Member
Join Date: Feb 2010
Location: The Moon
Old 12-16-2013 , 05:34   Re: Java help
Reply With Quote #4

Quote:
Originally Posted by nikhilgupta345 View Post
Look up the String split() function: it splits a string into an array of smaller strings, based on a certain delimiter (basically where you want to separate the strings from). You can make it so that it splits it based on a space, so you will get an array of words, in your case. Then take the 0 index and the length-1 index of that array, and those will give you the first and last words entered.
Hi nikilgupta345, would you mind creating an example?
__________________
jingojang is offline
Ade
I love purple
Join Date: May 2010
Old 12-16-2013 , 05:50   Re: Java help
Reply With Quote #5

Quote:
Originally Posted by jingojang View Post
Hi nikilgupta345, would you mind creating an example?
http://docs.oracle.com/javase/7/docs...lang.String%29
__________________
Ade is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 12-16-2013 , 20:57   Re: Java help
Reply With Quote #6

Quote:
Originally Posted by jingojang View Post
Hi nikilgupta345, would you mind creating an example?
PHP Code:
String s "This is a string";
String[] splitString s.split();
// splitString = {"This", "is", "a", "string"}
String firstWord splitString[0];
String lastWord splitString[splitString.length 1];

System.out.println("First word: " firstWord " Last word: " lastWord); 
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 12-17-2013 , 10:11   Re: Java help
Reply With Quote #7

Wow, I really had a blonde moment in my previous post, didn't I?
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
jingojang
Senior Member
Join Date: Feb 2010
Location: The Moon
Old 12-18-2013 , 06:10   Re: Java help
Reply With Quote #8

Thanks nikilgupta345

EDIT: nikilgupta345 you should be able to type in your own sentence, not put a sentence in a String. (JOptionPane.showInputDialog)
__________________

Last edited by jingojang; 12-18-2013 at 06:13.
jingojang is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 12-18-2013 , 17:35   Re: Java help
Reply With Quote #9

PHP Code:
String input JOptionPane.showInputDialog("Please input a string.");
String[] arrayOfWords input.split(" ");
System.out.println("First word: " arrayOfWords[0] + " Last word: " arrayOfWords[arrayOfWords.length 1]); 
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please

Last edited by nikhilgupta345; 12-18-2013 at 17:35.
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Reply


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


All times are GMT -4. The time now is 01:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode