Basically what I want to do is add a prefix (input by the user) to be added on a chosen word by the user. Is that posible? I got stuck and don't know what to do.... Here's what I have so far.

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

public class Prototype
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        String prefix;
        String word;
        String sentence;

        c.print ("Enter the prefix: ");
        prefix = c.readString ();
        c.print ("Enter the word to add the prefix to: ");
        word = c.readString ();
        c.print ("Enter the sentence: ");
        sentence = c.readString ();

        if (sentence == word)
        {
            c.print (prefix.concat (word));
        }

    } // main method
} // Prototype class

I know that my if statement is wrong... And yeah I got stuck..

Recommended Answers

All 13 Replies

Can you use the + operator?

public class StringThing
{
	public static void main(String[] args)
	{
		String strOne = "hey";
		String strTwo = "there";
		String strThree = strOne + ", " + strTwo;

		System.out.println(strThree);
	}
}
if (sentence == word)

Use the equals() method when comparing the contents of two String objects.
You use == mostly with primitives like int and char.

Yes I can use +. Well the thing is, is there a way I could add a string into a string a user inputted. Like say, if the prefix = re and the word = upload. Then the user input I want to upload. My program you print I want to reupload.

Have you tried to use the + operator to build the String you want from the other Strings that you have?

Yes it's basically the same as concat. How do I look for a certain word in a string? I tried using indexOf but doesn't seem to work.

There will be several methods.I'll tell you one way.You can seperate the string into characters by using charAt() method in java.Then store the characters in an array.Then you can do it easily.

How will charAt() work though. The user input will be different so charAt won't really work, right?

how about .contains() ?

Ready to Program Java IDE doesn't have .contains()

what's the IDE that you are using?

How do I look for a certain word in a string? I tried using indexOf but doesn't seem to work.

Please show the code you are using and what the results are when you use it.
Explain what is not working and what you want the code to do.
Show some examples.

indexOf would do exactly that, but if you don't have the same case (upper vs lower) in your original String and in the one you are searching, then it won't work.

@pro_learner: how would the choice of IDE (that is, if the OP even uses one) have any impact on this?

If case is not important to the search, then change the search arg and the lines that are being read to the same case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.