String java program help
Author |
Message |
shadow3dx
|
Posted: Wed Jan 11, 2012 1:38 pm Post subject: String java program help |
|
|
I'm stilll trying to familiarize myself with the language so encountering many problems in unavoidable. This is how the program is supposed to work:
? Display the input in all capital letters
? Display the input in all lower case letters
? Find the first location of a particular word that the user enters
? Determine the total number of times this word is found in the input
? Display the input will all a?s converted into e?s
? Determine the number of words input
? Determine the number of sentences input
How would start/work this out? Thanks a lot. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Wed Jan 11, 2012 2:45 pm Post subject: RE:String java program help |
|
|
Step 1: Read the documentation for the String class. I'll assume you're using Ready to Program, so the version of Java you want is 1.4.2. Link: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
Step 2: You can do most of the things required of you with exactly one method call on the relevant String object. For example, toUpperCase() and toLowerCase() are methods you could use.
Step 3: Some things will require a little bit more thought. You might have to use a for loop or a while loop, and maybe a counter variable. |
|
|
|
|
|
shadow3dx
|
Posted: Wed Jan 11, 2012 8:25 pm Post subject: Re: String java program help |
|
|
Ok I got everything. I started another part which asks me to ask for the users input and replace all vowels with an empty character.
this is what I have so far:
import hsa.Console;
public class StringAssignment2
{
public static void main (String[] atgs)
{
String shorten;
char Again;
Console c = new Console ("String Assignment continued");
c.println ("Please enter another word, the program will replace all vowels");
shorten = c.readLine ();
do
{
shorten.replaceAll ("u", "");
shorten.replaceAll ("o", "");
shorten.replaceAll ("i", "");
shorten.replaceAll ("e", "");
shorten.replaceAll ("a", "");
c.println ("If you want to continue press any key. If not press the star key (*/y)");
Again = c.getChar ();
}
while (Again == '*');
}
}
The problem is that it isn't replacing any vowels at all. What can I do? |
|
|
|
|
|
DemonWasp
|
Posted: Wed Jan 11, 2012 10:51 pm Post subject: RE:String java program help |
|
|
Read the documentation for the replaceAll() method VERY carefully. It does not modify the existing String. Instead, it creates a new String and returns that. So, for example:
Java: |
String a = "foo";
String b;
b = a.replaceAll ( 'o', 'u' );
// now a represents "foo"
// and b represents "fuu"
|
|
|
|
|
|
|
|
|