Hi guys, I'm kind of stuck on a problem I was assigned.
Pretty much we're trying to reverse words in a sentence.

As in, input = Hi I am new to Java
to:
Java to new am I Hi

I've tried this

String[]Tyler = new String[Sentence.length()];
	Tyler[0] = Sentence;

	for(int i=0;i<Sentence.length();i++)
	{
		if(Position[i] == -1)
			{
			break;
			}
				else
				{
					Position[i] = Tyler[i].indexOf(" ");
					Tyler[i+1] = Tyler[i].substring(Position[i]+1);
					count = count + 1;
				}
	}
	
	for(int i=0;i<4;i++)
	{
		System.out.println(Tyler[i]);
	}
	
	System.out.print(count);
	*/

And all that would do is simply COUNT the words. I would then have to find each individual word, place it into the array, reverse the array.

That's where I'm having trouble.
I cannot import ANYTHING. Has to be done with just basic EasyReader to read in the input and that's it.
I can use as many arrays as I want.

My idea was to find the first space and then by finding that space, I have my first word.
my first word would be:

Position[0] = Sentence.indexOf(" ");
Sentence.substring(0,Position[0]);

But I'm not sure where to work from there.
I don't really want the full solution, just how to get there.

This is my new, working, code for determining how many words in a sentence.
Making an improvement!

class Test {
    public static void main(String[] args) {
	System.out.println("Please enter a sentence. I will then reverse it for you.");
	EasyReader input = new EasyReader();
	String Sentence = input.readLine();
	
	int[]Word;
	Word = new int[Sentence.length()];
	int[]Position;
	Position = new int[Sentence.length()];
	int Count = 1;
	
	for(int i=0;i<Sentence.length();i++)
	{
		Position[i]=Sentence.indexOf(" ");
		Sentence = Sentence.substring(Position[i]+1);
		if(Position[i]== -1)
		{
		}
		else
		{
		System.out.println(Sentence);
		Count = Count +1;
		}
	}
	System.out.println(Count + " words are in this sentence. I will now reverse it.");
}
}

Nvm, found the solution.
Here's what I got, works perfectly.

class Test {
    public static void main(String[] args) {

	System.out.println("Please enter a sentence. I will then reverse it for you.");
	EasyReader input = new EasyReader();
	String Sentence = input.readLine();
	
	String[]Word;
	Word = new String[Sentence.length()];
	int[]Position;
	Position = new int[Sentence.length()];
	int Count = 1;
	Word[0] = Sentence;
	int Check;
	Check = 0;
	for(int i=0;i<Sentence.length();i++)
	{
		Position[i]=Word[i].indexOf(" ");
		Check = Position[i];
		if(Check == -1)
		{
		break;
		}
		else
		{
		Word[i+1] = Word[i].substring(Position[i]+1);
		//System.out.println(Word[i+1]);
		Count = Count +1;
		}
	}
	/*for(int i=0;i<Count;i++)
	{
		System.out.println(Position[i]);
	}
	*/
	System.out.println(Count + " words are in this sentence. I will now reverse it.");
	
	for(int i=0;i<Count-1;i++)
	{
		Word[i] = Word[i].substring(0,Position[i]);
	}
	
	while(Count > 0)
	{
		System.out.print(Word[Count-1] + " ");
		Count = Count -1;
	}
}
}

Hello - nice to see that you solved your code however you could condense this abit and still use no imports. Here's the code:

public static void reverseStringTakeTwo( String line )
	{
		// 'input.split("")' splits the string into an array, therefore each character inhabits a position in the array
		String[] letter = line.split("");
		for( int numChars = letter.length-1; numChars > 0; numChars-- )
		{
			System.out.print(letter[numChars]);
		}
	}

Hope this helps a-bit if even it simply provides another way of doing it.

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.