0

Hi, I need help with arrays. Here's what I have so far....

// The "Prefixes123" class.
import java.awt.*;
import hsa.Console;

public class Prefixes123
{
    static Console c;
    
    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.equals (word))
                c.print (prefix + arraystr [i]);

    } // main method
} // Prefixes123 class

What I need to be doing is adding a prefix into a word inside a string on its every occurrence. My problem is I don't know how to call the arrays before the word and after. NOTE: I'm not allowed to use any of the String methods (like .replace()) except for charAt() and .length.

Edited by Daigan: n/a

3
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by Daigan
Featured Replies
  • 1

    Maybe seeing it this way will demonstrate better... [CODE]for (int i = 0 ; i < arraystr.length ; i++) { if (arraystr [i].equals (word)) c.print (prefix); // this will display the prefix ONLY if it finds the search word c.print (arraystr [i] + " "); // this will display EVERY … Read More

0

since you are already using 'split', I would guess you're ... heading for a fall, as the song says.

I'm not sure what you mean by 'calling the arrays before the word and after', but I'll tell you what to do if you mean what I think you mean:

1. place your original sentence in a separate array
2. place the word you are looking for in a separate String
3. loop over your array
if ( current element equals the word you look for)
change value to "prefix " + " currentValue"

0

@stultuske Actually, that IS what the OP is trying to do.

Line #28 should be using your index.

if (arraystr[i].equals (word))  // IF you need to...
   c.print (prefix);     // ...print your prefix.
c.print (arraystr[i]); // Print the "words" of your string.

However, you will have to add spacing accordingly.

Edited by hfx642: n/a

0

since you are already using 'split', I would guess you're ... heading for a fall, as the song says.

I'm not sure what you mean by 'calling the arrays before the word and after', but I'll tell you what to do if you mean what I think you mean:

1. place your original sentence in a separate array
2. place the word you are looking for in a separate String
3. loop over your array
if ( current element equals the word you look for)
change value to "prefix " + " currentValue"

Could you provide an example, please?

@hfx642

What I need to do is to print the words in the array before the word and after the word with the added prefix. So let's say I entered "I want to upload". The chosen word and prefix are upload and re. The result would be I want to reupload. Sorry if I didn't elaborate from my previous post.

0

Understood... and the snippet of code I gave you will achive the desired result.
However, you will also have to accommodate for the desired spacing between the words.
You could do this by adding a space at the end of printing your word.

c.print (arraystr[i] + " ");

Edited by hfx642: n/a

0

There's an error when I tested out the code you gave me.

c.print (arraystr[i] + " ");

. An ArrayIndexOutOfBoundsException error? And from what I understand of the code you gave me. Shouldn't it just print the prefix + the word from the array? So wouldn't it be the same as

c.print (prefix + arraystr[i] + " ");

?

Edited by Daigan: n/a

0

1. You forgot to put your "{}" around the code for your "for loop".
2. No. Your way will print ONLY the prefix and word.
My way will print every word in the sentence, including the prefix.

0

Thank you so much. But could explain to me why it adds the prefix to just the word and not at the beginning of the sentence. Because from what I understand this code:

if (arraystr [i].equals (word))
                c.print (prefix);
            c.print (arraystr [i] + " ");

Should print the prefix first then the rest of the array.

1

Maybe seeing it this way will demonstrate better...

for (int i = 0 ; i < arraystr.length ; i++)
{
   if (arraystr [i].equals (word)) c.print (prefix);  // this will display the prefix ONLY if it finds the search word
   c.print (arraystr [i] + " ");  // this will display EVERY word
}

The way that you had it...

for (int i = 0 ; i < arraystr.length ; i++)
   if (arraystr.equals (word)) c.print (prefix + arraystr [i]);

will only display the prefix and the search word.

Edited by hfx642: n/a

0

Sorry I forgot that I need to return the new sentence instead of just using print. And I have no idea how to do it.
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 by Daigan: n/a

0

a) Please mark this thread as "solved".

b) Please don't post the same problem in multiple threads.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.