12 Replies - 16825 Views - Last Post: 03 February 2009 - 04:09 AM Rate Topic: -----

#1 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Printing array in reverse order

Posted 23 January 2009 - 01:15 PM

hi, i would like some help with my yodaspeak program. I have done most of it but i cant seem to print the user inputs sentence backwards. it only prints the odd words and leaves the even number of words.
i.e if you type 'he has turned to the dark side' the terminal window shows he turned the side.

import java.util.Scanner; //Scanner object allows us to read from the keyboard
import java.util.StringTokenizer;

public class YodaSpeak2
{
	public static void main (String [] args) {
		
		String line;	//We will read an entire line from 
		int i=0;
		int a;
		String inputArray[] = new String [20]; //Array of strings, maximum of 20 elements long
		
		System.out.println("Type in your sentence"); //Prompts the user
		Scanner input = new Scanner(System.in); // Now create a scanner object and 
		line = input.nextLine(); //read the entire line from the line
		input.close(); // good practice to close the scanner object
		
		StringTokenizer splitMe = new StringTokenizer (line);
		
	  a = splitMe.countTokens();
		while (splitMe.hasMoreTokens() & (i<20) ) {  
			  inputArray [i] = splitMe.nextToken();
			  i++; 
			}   
			System.out.println("Finished processing" + a + "words");
			
			
		for (i=0;i<a;i++) {
			System.out.print(inputArray [i] + 1);
			i++;
		 }
	 }
}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Printing array in reverse order

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing array in reverse order

Posted 23 January 2009 - 01:24 PM

Welcome!

When posting code examples, please use the code tags:

:code:

Doing so makes it much easier to read.

You can edit your post to add the tags.

Thanks!

This post has been edited by n8wxs: 23 January 2009 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Printing array in reverse order

Posted 23 January 2009 - 01:24 PM

ok, welcome to DIC. here's an easy way to do this
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String [] a = line.split(); 
for(int i = a.length -1; i >= 0; i--)
{
	 System.out.println(a[i]);
}


the split() method, splits any string into an array of Strings with space as a delimiter..what i just did is that i looped through the array starting from the last element and moving towards the beginning.. i hope that's what you were trying to do
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Printing array in reverse order

Posted 23 January 2009 - 01:58 PM

Picking up on what mostyfriedman wrote, StringTokenizer is deprecated being replaced by the split() method of the String Class.

The reason you were skipping words in your output loop is that you are double incrementing the loop counter i:
 for (i=0;i<a;i++) {
System.out.print(inputArray [i] + 1);
i++;
}


Here's your code modified a bit.
...
a = splitMe.countTokens();
// Use the array length rather than hard coding the size. That way
// you can change the size of the array and this code will still work
if (a > inputArray.length) {
	System.out.println("Your input text is too long! Enter a shorter text.");
	return;
}
// We don't need the while() loop as we know the number of tokens
// and we know the copied tokens will fit in the array
for (i = 0; i < a; i++) {
	inputArray[i] = splitMe.nextToken();
}
...
System.out.println("Finished processing " + a + " words");
// Show the tokens
for (i = 0; i < a; i++) {
	System.out.print(inputArray[i] + " ");
}
System.out.println();
// Show the tokens in reverse order
for (i = a - 1; i >= 0; i--) {
	System.out.print(inputArray[i] + " ");
}
System.out.println();
...


Was This Post Helpful? 0
  • +
  • -

#5 NuclearPeon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 08-January 09

Re: Printing array in reverse order

Posted 23 January 2009 - 02:02 PM

With normal printing of an array, you start at 0 and work your way up the array.

Henceforth,

printing an array backwords involves starting at the last element and working your way back.

You may find array.length and i-- very important tools in your quest.
Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Printing array in reverse order

Posted 23 January 2009 - 02:10 PM

What exactly do you mean by yoda speak?
Because if you do Yoda speak, it generally means sentences in a modified version of the Object-Agent_Verb(OAV) form.

So it is alot more complicated then simply reversing the sentence,

ie this he has turned to the dark side would be Turned to the dark side, he has.

So do you just wish to reverse the strings, or do something like above?
Was This Post Helpful? 0
  • +
  • -

#7 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: Printing array in reverse order

Posted 25 January 2009 - 02:05 PM

thanks a lot for your help. I didnt really want it in reverse orde rjust a differnt order, like how yoda from star wars speaks but you have given me more than enough help. Thanks!

And bigant, i did want to do exactly what you had written down including commas and stuff but it still was a lot of help given my everyone.
Was This Post Helpful? 0
  • +
  • -

#8 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: Printing array in reverse order

Posted 26 January 2009 - 08:33 AM

bigAnt, could you show me how to do the code how you said, like when the user types 'he has turned to the dark side' and the output is 'Turned to the dark side, he has.' including commas and full stops if it is possible.
Thanks
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Printing array in reverse order

Posted 26 January 2009 - 09:52 AM

Yoda speak is essentially Japanese.

It's a moving of the object verb order. So something like "I will go tomorrow" becomes "Tomorrow, I will go" (ashita wa ikimasu). Well, except that Japanese drop the subject most of the time, and hate pronouns; there's a "watashi" inferred in there.

Found an excellent run down of it here: http://www.tofugu.co...nese-from-yoda/

Part of the point here is, natural language parsing is a beast and very few ever really get it right.
Was This Post Helpful? 0
  • +
  • -

#10 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: Printing array in reverse order

Posted 27 January 2009 - 01:37 PM

does anyone know how to split tokens in half and print the second half of the token first and then the first half. Meaning split the word he has turned to the dark side as 'Turned to the dark side, he has' with the commas and capital letters.
Was This Post Helpful? 0
  • +
  • -

#11 NuclearPeon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 08-January 09

Re: Printing array in reverse order

Posted 27 January 2009 - 03:35 PM

Quote

does anyone know how to split tokens in half and print the second half of the token first and then the first half. Meaning split the word he has turned to the dark side as 'Turned to the dark side, he has' with the commas and capital letters.


"he has turned to the dark side" isn't one word, so I'm a little confused.

If you wanted to split one token, aka, one word of the phrase into two pieces, you would take the one token, take the substring of that token to its ' length() / 2 ' value, then substring the rest of it to have two parts. Then it's just a matter of printing out the second substring first and the other second.

Am I getting warm?
Was This Post Helpful? 0
  • +
  • -

#12 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: Printing array in reverse order

Posted 27 January 2009 - 04:58 PM

Yeah he would have to devise his own parser to be able to return the Object the Agent and the Verb components and properly reorder them.
Was This Post Helpful? 0
  • +
  • -

#13 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: Printing array in reverse order

Posted 03 February 2009 - 04:09 AM

I'm not sure how to use the object verb. I would just like to split the tokens in half asnd print the second half first, then the first half. Wouldn't u creates 2 loops and use the StringTokenizer to split the tokens in half.

It would bea bit easier if you can explain through code because i'm not sure i understand the verb technique.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1