Here's what I have so far..

// The "AddingPrefixes" class.
    import hsa.*;
    import javax.swing.JOptionPane;
     
    public class AddingPrefixes
    {
    static Console c; // The output console
    static char proc;
    static String prefix;
    static String word;
    static String sentences;
     
    private void title ()
    {
    c.print (' ', 32);
    c.println ("Adding a Prefix");
    c.println ();
    }
     
     
    public void intro ()
    {
    c = new Console ();
    title ();
     
    c.println ("This program allows you to add a prefix of your choice to a word in a sentence that you input.");
    c.println ();
    c.print ("To continue input 'p'");
     
    while (true)
    {
    try
    {
    proc = c.readChar ();
    if (proc == 'p')
    c.print ("lol");
    else
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    }
    }
     
     
    public void mainmenu ()
    {
    c = new Console ();
    title ();
     
    c.print (' ', 18);
    c.println ("The following are the choices for the program: ");
    c.println ();
    c.print (' ', 18);
    c.println ("To continue : 'p'");
    c.print (' ', 18);
    c.println ("To go through the introduction : 'i'");
    c.print (' ', 18);
    c.println ("To exit the program : 'x'");
    c.println ();
    c.print (' ', 18);
    c.print ("Enter your choice: ");
     
    while (true)
    {
    try
    {
    proc = c.readChar ();
    if (proc == 'p')
    askData ();
    if (proc == 'i')
    intro ();
    if (proc == 'x')
    goodbye ();
    else
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    catch (Exception e)
    {
     
    }
    }
    }
     
     
    public void askData ()
    {
    c = new Console ();
    title ();
     
    c.print (' ', 18);
    c.print ("Enter the prefix: ");
    while (true)
    {
    try
    {
    prefix = c.readLine ();
    break;
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    }
     
     
    c.print (' ', 18);
    c.print ("Enter the word: ");
    while (true)
    {
    try
    {
    word = c.readLine ();
    break;
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    }
     
     
    c.print (' ', 18);
    c.print ("Enter the sentence: ");
    while (true)
    {
    try
    {
    sentences = c.readLine ();
    displayData();
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "That's not one of the options. Please Try Again");
    }
    }
    }
     
     
    private String prefix (String newS, int i)
    {
     
    String[] arraystr = sentences.split (" ");
     
    for (i = 0 ; i < arraystr.length ; i++)
    {
    if (arraystr [i].equals (word))
    c.print (prefix);
    c.print (prefix + arraystr [i] + " ");
     
    }
    newS = (prefix) + (arraystr [i] + " ");
     
    return (newS);
    }
     
     
    public void displayData ()
    {
    c = new Console ();
    title ();
     
    c.print ("The sentence(s) you typed in is/are " + sentences + " is/are converted to" + prefix ("null", 0));
    }
     
     
    public void goodbye ()
    {
    c = new Console ();
    title ();
    c.print ("This is the end of the program. Thank you for using it!");
    }
     
     
    public static void main (String[] args)
    {
    AddingPrefixes d;
    d = new AddingPrefixes ();
    d.mainmenu ();
     
    } // main method
    } // AddingPrefixes class

My problem is at the prefix method. I got mixed up there and got stucked. I don't know how to return the new arrays with the prefix.

Edited 5 Years Ago by Daigan: n/a

I don't know how to return the new arrays with the prefix.

Please explain your problem.

Do you really need all this code for coding a return statement?

Do you really code this way with every statement starting in the first column?
That makes it very hard to read and understand your code.

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]+ " ");
    }
 
    }

Sorry about that. Here's a simpler version. What that code does is basically adding a prefix to word chosen by the user. My problem is that I can't use print, instead I need to use return.

Edited 5 Years Ago by Daigan: n/a

Then replace the printing code with something like this:

return theStringToBeReturned;

You do not say what String you want to return. Can you write code to create the String you want to return?
If so, then use that String with the return statement as shown above.

Yes, that's what I tried but I think It needs to be outside the loop. Because there's an error saying that the method needs a return statement compatible with String.

Describe what the loop does and where you want to build the String and what String you want to return.

You can do a return from inside the loop with one value
and/or a return from outside the loop with the same or another value.

Build the String anywhere in the method and return the String at the end of the method.

Okay so the loop does is to keep looping til I find the array with a certain word then adds a prefix to it. Does it matter where I build the String? And I want the string to return the arrays with the prefix added on to a certain in the arrays.

oes it matter where I build the String?

No. The only requirement is that you have the values you need to build it.

I want the string to return the arrays with the prefix added on to a certain in the arrays.

You can build as large a String as you want. That should not be a problem.

How do I build the String with the previous array and the array with the prefix added to it?

What is the "previous array"?
Can you give some examples of the contents of these arrays and the value of the prefix and show what the final built String will look like?

You build a String by concatenating other Strings to it. To get the contents of a String array, you will need to loop through the array and get the elements out of it one by one.

Say the user input for prefix is "re" and for word is "upload" and the sentence is "I want to upload". The final built String would be then "I want to reupload".

Edited 5 Years Ago by Daigan: n/a

prefix is "re"
and for "word" is upload
and the sentence is "I want to upload"

You will need logic to find where to put the prefix.
There are methods in the String class for finding the location of word in sentence
and for getting the substrings from sentence.

Your problem is how to take three Strings and merge one of them into another using a location provided by a third.

Yeah and I was able to do that with the for loop and if statement. I'm also not allowed to use any builtin string methods except for charAt and length. The only problem is instead of using print is that I need to return the value.

Where are the parts of the String that you have to work with? Use them to build the String that you want to return.

I'm also not allowed to use any builtin string methods except for charAt and length.

Does that mean you have to write your own indexOf() and substring() methods to solve your problem? That can be done with some loops and if statements.

Edited 5 Years Ago by NormR1: n/a

The strings that im working with have to be user inputs.

I think that's what I did on my prefix method its just that instead of printing it i need to return it

The strings that im working with have to be user inputs.

That has nothing to do with how you solve the problem.
At some point you will have the three Strings. Their source won't be important.

instead of printing it i need to return it

Change the print statemnt to an assignment statement:
print(theString); // print the String
change to:
String strToReturn = theString; // save the String


then return strToReturn; // return it

Edited 5 Years Ago by NormR1: n/a

Well I have print (prefix) and print (arraystr + " ") how am i going to return 2 values in a single e method

print (prefix) and print (arraystr + " ")

theStringToReturn = prefix + arraystr + " "; // build the String to return

then
return theStringToReturn; // return the String

Yes I tried that but it doesn't do what I want it to do. By using that I only get the prefix + word not the whole sentence. That's why I had two print statements.. I also can't return two values on the same method..

I also can't return two values on the same method..

Return one value by concatenating the two Strings together so that there is only one value.

By using that I only get the prefix + word not the whole sentence.

Use concatenation to get all the parts you want to be returned.

As an exercise: write a simple program with a loop that builds a String with the odd values of the loop index. Have the loop iterate about ten times.
The built String should be: "13579"
The code in the loop needs to test the value of the loop index and only concatenate the odd values. If you can write this simple program you should be able to write the code you need in your program.

Im really stuck

I'm not sure where your problem is. Which of the following are a problem:
Can you write a loop?
Can you concatenate one String to another?
Can you test if a number is even or odd (hint use %)
Can you use an if statement to determine if you should execute another statement or not?

If you can do all those you should be able to write the simple test program I suggested you write.
Post the code that you are working for the simple program and ask about what you are stuck on.

Thanks for your advices I was able to figure it out. :)

This question has already been answered. Start a new discussion instead.