4 Replies - 8532 Views - Last Post: 05 February 2009 - 07:15 PM Rate Topic: -----

#1 nbl0066   User is offline

  • New D.I.C Head

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

Reverse strings line by line

Post icon  Posted 05 February 2009 - 05:56 PM

I am trying to construct my method to reverse a .txt file line by line. For example:

"This is a line"
needs to be
"line a is This"

We must do this without using .split or any other classes in java so I am just using substring.

Here is the code for my method and beneath that is what it is outputting. I am struggling to figure out why it is reading the words like it is.

The array of words holds the following .txt file with each element being a line of text. int lines is the number of lines which is 14.

"Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross'd lovers take their life;
Whole misadventured piteous overthrows
Do with their death bury their parents' strife.
The fearful passage of their death-mark'd love,
And the continuance of their parents' rage,
Which, but their children's end, nought could remove,
Is now the two hours' traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend."

public void reverseText(String[]words, int lines, String line)
		{
			int lastSpace = 0;
			int i;
			char ch;
			String word;
			String str = " ";
			
			for(int j = 0; j < lines; j++)
			{
				str = words[j];
				lastSpace = words[j].length();
				for(i=lines-1; i >= 0; i--)
				{
					ch = str.charAt(i);
					if(ch == ' ')
					{
						word = str.substring(i + 1, lastSpace);
						System.out.print(word + " ");
						lastSpace = i;
					}
				}
				word = str.substring(0, lastSpace);
				System.out.println(word);
			}

		}



Here is the output:

"households, both alike in dignity, Two
Verona, where we lay our scene, fair In
grudge break to new mutiny, ancient From
blood makes civil hands unclean. civil Where
the fatal loins of these two foes forth From
star-cross'd lovers take their life; of pair A
misadventured piteous overthrows Whole
death bury their parents' strife. their with Do
passage of their death-mark'd love, fearful The
continuance of their parents' rage, the And
their children's end, nought could remove, but Which,
two hours' traffic of our stage; the now Is
you with patient ears attend, if which The
shall miss, our toil shall strive to mend. here What"

Is This A Good Question/Topic? 0
  • +

Replies To: Reverse strings line by line

#2 nbl0066   User is offline

  • New D.I.C Head

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

Re: Reverse strings line by line

Posted 05 February 2009 - 06:13 PM

hopefully before anyone starts working on responding... i got it figured out! I do have another problem with another method so I am about to post that
Was This Post Helpful? 0
  • +
  • -

#3 nbl0066   User is offline

  • New D.I.C Head

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

Re: Reverse strings line by line

Posted 05 February 2009 - 06:26 PM

I am now trying to complete my method dealing with finding unique words. I am using the same .txt file as above. We are to loop through the entire array (one line of text is one element of the array) and save the unique words into another array. We are basically removing the duplicates but I am thinking of it as actually finding the non-duplicates. So far I have this code that is not working. I believe I need to add .split into it and probably another loop but I'm having a hard time getting it all together. Here is my code so far:

public String uniqueWords(String[]words)
	{
	   
	   String[]uniqueWords = new String [5000];
	   int uniqueCount = 0;
	   
	   boolean found = false;
	   
	   for(int i=0; i < words.length; i++)
	   {
		   for(int j=0; j <= uniqueCount; j++)
		   {
			   if (words[i].equalsIgnoreCase(uniqueWords[j]))
			   {
				   found = true;
			   }
			   if (!words[i].equalsIgnoreCase (uniqueWords[j]))
			   {
					   uniqueWords[uniqueCount]=words[i];
					   uniqueCount++;
			   }
		   }
		}

	   return uniqueWords[0];
	}


Also may I add that I need to ignore puncuation and capitalization.
Was This Post Helpful? 0
  • +
  • -

#4 nbl0066   User is offline

  • New D.I.C Head

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

Re: Reverse strings line by line

Posted 05 February 2009 - 06:51 PM

anyone...?
Was This Post Helpful? 0
  • +
  • -

#5 markhazlett9   User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Reverse strings line by line

Posted 05 February 2009 - 07:15 PM

Ok woa... one issue at a time here... First of all for you reversing every line issue... You will create a method to reverse a single string, Then just use a for loop to loop through depending on the amount of String's you need to loop through... Reading a different line in every time.

As for your second issue... You will need to split every single word out of the file using either a tokenizer or a split using a space as the delimeter. Then compare each of them against whatever and if they match store that word in a new array. Else throw it away. Hope this helps... Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1