// The "Prefixes123" class.
import java.awt.*;
import hsa.Console;
public class Prefixes123
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
String prefix;
String word;
String sentences;
c.print ("Enter the prefix: ");
prefix = c.readLine ();
c.print ("Enter the word: ");
word = c.readLine ();
c.print ("Enter the sentence: ");
sentences = c.readLine ();
String[] arraystr = sentences.split (" ");
for (int i = 0 ; i < arraystr.length ; i++)
{
if (arraystr [i].equals (word))
c.print (prefix);
c.print (arraystr [i] + " ");
}
} // main method
} // Prefixes123 class
Hi, so I was able to do what I want but in a wrong way.. Basically what the program does it get a prefix and the word to add it into. Then the sentence. Then the sentence is split into an array. Then the loops finds the word in the array and add the prefix to it. I was able to do it and print. But my problem is that I need to return it instead of print. How am I going to do it? Sorry I'm new with java.
2 replies to this topic
#1
Posted 27 November 2011 - 08:36 PM
|
|
|
#2
Posted 28 November 2011 - 07:32 AM
Ignoring your code for a moment. If you want to return a string then you should put all that logic in a method
public String whatEverYouAreDoing(String word, String prefix, String sentence)
{
//logic
return (a string);
}
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#3
Posted 29 November 2011 - 04:38 AM
All you need to do is to compose back the original string using "+" operator to concatenate strings.
...
String[] arraystr = sentences.split (" ");
String new_sentence = "";
for (int i = 0 ; i < arraystr.length ; i++)
{
if (arraystr [i].equals (word))
arraystr[i] = prefix + arraystr[i];
new_sentence += (arraystr [i] + " ");
}
}
return new_sentence;
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









