The Artima Developer Community
Sponsored Link

Java Answers Forum
The trim()

19 replies on 2 pages. Most recent reply: Jan 14, 2005 2:03 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 19 replies on 2 pages [ 1 2 | » ]
Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

The trim() Posted: Jan 4, 2005 2:48 AM
Reply to this message Reply
Advertisement
Hi, everybody
Could someone help me please?
First- I'm utilising HashMap to stored and retrieved information such as Characters and their appearances in a text. When compiling the program, I've got this message:
-Note: CipherDemonstration.java uses unchecked or unsafe operations.
-Note: Recompile with -Xlint: unchecked for details.
Second-I’m trying to utilise trim as following:
-myTrimWorm = passedText.trim ()-But nothing happening, this does not work, please help.
The main purpose of the program:
-Take a plain text,
-analyse it
-display the character (e.g: a,b,etc.) and the number of times its appear (e.g: a-20)
-display it percentage!! (of appearances)
-thus, trim (omit) all Whitespace character.
Dough...:=)
Many thanks


Jeremy Stein

Posts: 4
Nickname: jstein
Registered: Jan, 2005

String.trim method Posted: Jan 4, 2005 6:23 AM
Reply to this message Reply
The trim method removes leading and trailing whitespace. Perhaps you were expecting it to remove internal whitespace?

Throw in some println statements like this:
System.out.println("Original: " + passedText);
myTrimWorm = passedText.trim();
System.out.println("Trimmed: " + myTrimWorm);

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 4, 2005 10:33 AM
Reply to this message Reply
Many thanks for your attention,

i Trim () Returns a copy of the string, with leading and trailing whitespace omitted.
// Create a new Object from myOutputMessage2 as String
outputTrim = new String ( myOutputMessage2 );
String finalOutput = "";
finalOutput = outputTrim.trim ();
System.out.println ( "The finalOutput after Trim is: " + finalOutput );
/i

This is what I have in my Program, please let me know if your can helping me then.

Jeremy Stein

Posts: 4
Nickname: jstein
Registered: Jan, 2005

Re: String.trim method Posted: Jan 4, 2005 10:44 AM
Reply to this message Reply
What is the output of those println calls?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: String.trim method Posted: Jan 4, 2005 2:46 PM
Reply to this message Reply
Huh?

Maybe you could explain (cohesively, if possible) what exactly you are trying to achieve.

If you are trying to remove all the whitespace in a string, look at String.replace() and/or String.replaceAll().

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: The trim() Posted: Jan 5, 2005 2:18 AM
Reply to this message Reply
With regards to the trim() my 2 cents on it are as follows:

If its compiling, then you should be pretty much set
(I don't see why trim would not be working as you
are expecting it to or as you explained you are
expecting it to).

If NOT, then you are probably not trying trimming a
String Object. I've come across Swing scenarios
in which my compiler has been a little bit idiotic
and where there has been a getText() expecting to
return a String, I've had it say "hey you're not really
trying to trim a String!!"

I've fixed such problems by Stringing the Object before
trimming. Like so:

ObjectImTrimming.toString().trim();

It has worked for me... But then I might be
misunderstanding your problem...

One,
Spike

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: The trim() Posted: Jan 5, 2005 10:11 AM
Reply to this message Reply
Many thanks for your attention,

//Trim () Returns a copy of the string, with leading and trailing whitespace omitted.
// Create a new Object from myOutputMessage2 as String
outputTrim = new String ( myOutputMessage2 );
String finalOutput = "";
finalOutput = outputTrim.trim ();
System.out.println ( "The finalOutput after Trim is: " + finalOutput );


This is what I have in my Program, please let me know if your can helping me then. Nevertheless, I will try to night your solution with Object.
:=), thanks.

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 5, 2005 10:12 AM
Reply to this message Reply
Many thanks for your attention,

// Trim () Returns a copy of the string, with leading and trailing whitespace omitted.
// Create a new Object from myOutputMessage2 as String
outputTrim = new String ( myOutputMessage2 );
String finalOutput = "";
finalOutput = outputTrim.trim ();
System.out.println ( "The finalOutput after Trim is: " + finalOutput );

This is what I have in my Program, please let me know if your can helping me then.

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 6, 2005 4:46 AM
Reply to this message Reply
Many thanks for your attention
my code is:
//Trim () Returns a copy of the string, with leading and trailing whitespace omitted. 
// Create a new Object from myOutputMessage2 as String
outputTrim = new String ( myOutputMessage2 );
String finalOutput = "";
finalOutput = outputTrim.trim ();
System.out.println ( "The finalOutput after Trim is: " + finalOutput );


Thanks

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 6, 2005 4:52 AM
Reply to this message Reply
Please mat,
In addition, I'm utilising a HasMap for storing, and retrieving the information. I have to display in a TextArea the result like that:
Number Frequency
Occurences Percentage
a: 23 11%
All the above works, except the Percentage part. I can get the real amount, instead I've got 0% allways.

Please could you explain it to me, many thanks

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 6, 2005 4:55 AM
Reply to this message Reply
Please mat, Cofused for the Formatting things
Here we are again...

In addition, I'm utilising a HasMap for storing, and retrieving the information. I have to display in a TextArea the result like that:
Number Frequency
Occurences Percentage
a: 23 11%

All the above works, except the Percentage part. I can get the real amount, instead I've got 0% allways.

Please could you explain it to me, many thanks

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: String.trim method Posted: Jan 6, 2005 5:27 AM
Reply to this message Reply
What is ur problem?

What is the input? what output u are expecting?

What is the code that u r using?

U will surely get the reply if u can post the answers to above questions..

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 8, 2005 5:59 AM
Reply to this message Reply

Many thanks for your advices, replaceAll() does work properly,
thus could you give a look to this code for me.
- I would like to output in a JTextArea this format:

Number Frequency
Occurences Percentage
a: 23 18%

this is an Example of string in output utilising the java program bellow.

    /**
     *  performPatternAnalyse Method
     * 
     */
	public void performPatternAnalyse ( int passedToggle )
	{
		HashMap myFrequency = new HashMap ();
		Integer ctrCount = 0;
		Integer myFinalCTR_1 = 0;
		Integer myFinalCTR_2 = 0;
		Integer myPercent_1 = 0;
		Integer myPercent_2 = 0;
		String analyse1 = "";
		String analyse2 = "";
		String analyse3 = "";
		
		if ( passedToggle == 1 )
		{
			char [] myChars = displaySwapTA_4_2.toCharArray ();
			for ( int ctr = myChars.length-1; ctr >= 0; ctr-- )
			{
				Character theCharacter = new Character ( displaySwapTA_4_2.charAt ( ctr ) );
				ctrCount = ( Integer ) myFrequency.get ( theCharacter );
				if ( ctrCount == null )
				{
					myFrequency.put ( theCharacter, new Integer ( 1 ) );
				} 
				else {
					myFrequency.put ( theCharacter, new Integer ( ctrCount.intValue () +1 ) );
					myFinalCTR_1 += ctrCount.intValue () +1;
				}
			}
			Iterator myIter = myFrequency.keySet ().iterator ();
			Object myCharKey;
	
			displayOutput4.setText ( ("------  Text First Analysed ------\n") + 
				( " Name:\t\t" + "Frequency"+ "\n" ) +
				( "\tOccurences\t" + "Percentage"+ "\n" ) );
			while ( myIter.hasNext () )
			{
				myCharKey = myIter.next ();
				myPercent_1 = ( ( ctrCount * 100 ) / myFinalCTR_1 );
				displayOutput4.append ( " " + myCharKey + " :\t" + myFrequency.get ( myCharKey ) 
				+ "\t\t" + myPercent_1 + "\n" );
			}
			toggleAnalyse = 2;
		}
		else if ( passedToggle == 2 ) {
			char [] myChars = displaySwapTA_4_3.toCharArray ();
			for ( int ctr = myChars.length-1; ctr >= 0; ctr-- )
			{
				Character theCharacter = new Character ( displaySwapTA_4_3.charAt ( ctr ) );
				ctrCount = ( Integer ) myFrequency.get ( theCharacter );
				if ( ctrCount == null )
				{
					myFrequency.put ( theCharacter, new Integer ( 1 ) );
				} 
				else {
					myFrequency.put ( theCharacter, new Integer ( ctrCount.intValue () +1 ) );
					myFinalCTR_2 += ctrCount.intValue () +1;
				}
			}
			Iterator myIter = myFrequency.keySet ().iterator ();
			Object myCharKey;
			
			displayOutput4.append ( ( "\n" + "------  Text Second Analysed ------\n") + 
				( " Name:\t\t" + "Frequency"+ "\n" ) +
				( "\tOccurences\t" + "Percentage"+ "\n" ) );
			while ( myIter.hasNext () )
			{
				myCharKey = myIter.next ();
				myPercent_2 = ( ( ctrCount * 100 ) / myFinalCTR_2 );
				displayOutput4.append ( " " + myCharKey + " :\t" + myFrequency.get ( myCharKey ) 
					+ "\t\t" + myPercent_1 + "\n" );
			}
			toggleAnalyse = 0;
		}	
		else {
			Toolkit.getDefaultToolkit ().beep ();
			JOptionPane.showMessageDialog ( null, "Please First Enter the Text to Analyse... ", 
				" Information Message ", 
			JOptionPane.INFORMATION_MESSAGE );
			toggleAnalyse = 0;
               setCursor ( null );
		} // End for If-Else
	}	// End performPatternAnalyse Method


The Analyse works properly, but I've got aproblem
withe the percentage result: 0...
Could someone gives some little helps...Many thanks All.
[pre/]

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: String.trim method Posted: Jan 8, 2005 11:12 AM
Reply to this message Reply
Well, if you had said in the first place what your intent was, the question would probably have been answered on the first reply.

If you can't make the effort to communicate a little about what exactly you are trying to do, it seems like a stretch to expect people to read your code, try to divine your intent and explain how to fix it.

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 12, 2005 2:15 AM
Reply to this message Reply
Thanks,
What I would like to do from the Java Code I did provide:

-First, display in output (TextArea) in this format

Numbers Frequency
Occurencies Percentage
a: 23 18%(this is an example of output.)

All above working properly, except the Percentage, which is
display o% in output.

I'm utilising Hashmap to store the information in...

Flat View: This topic has 19 replies on 2 pages [ 1  2 | » ]
Topic: Final Classes in Java Previous Topic   Next Topic Topic: Need help in killing thread

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use