AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   Java help (https://forums.alliedmods.net/showthread.php?t=231341)

jingojang 12-12-2013 13:17

Java help
 
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()));
    }



Powerlord 12-12-2013 14:23

Re: Java help
 
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...

nikhilgupta345 12-14-2013 11:50

Re: Java help
 
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.

jingojang 12-16-2013 05:34

Re: Java help
 
Quote:

Originally Posted by nikhilgupta345 (Post 2071947)
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?

Ade 12-16-2013 05:50

Re: Java help
 
Quote:

Originally Posted by jingojang (Post 2072636)
Hi nikilgupta345, would you mind creating an example?

http://docs.oracle.com/javase/7/docs...lang.String%29

nikhilgupta345 12-16-2013 20:57

Re: Java help
 
Quote:

Originally Posted by jingojang (Post 2072636)
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); 


Powerlord 12-17-2013 10:11

Re: Java help
 
Wow, I really had a blonde moment in my previous post, didn't I?

jingojang 12-18-2013 06:10

Re: Java help
 
Thanks nikilgupta345

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

nikhilgupta345 12-18-2013 17:35

Re: Java help
 
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]); 



All times are GMT -4. The time now is 14:28.

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