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 ]
John Neale

Posts: 10
Nickname: rhino
Registered: Oct, 2003

Re: String.trim method Posted: Jan 12, 2005 4:44 AM
Reply to this message Reply
Advertisement
Looks to me like you are trying to do division on ints. Try converting the frequencies to float or double to do the division and then round these values to the number of decimal places you require.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: String.trim method Posted: Jan 12, 2005 11:40 AM
Reply to this message Reply
Or if you like rough percentages (truncated instead of rounded, by the way), you can always mulitply by 100 first:
int roughPercent = 100 * count / total;

(of course, an asute observer might notice that the combined percentages don't add up to 100%, but that could happen with rounding, as well, even if it is less likely)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: String.trim method Posted: Jan 12, 2005 5:59 PM
Reply to this message Reply
Hmm... Having taken a little closer look at the code you posted, I am very skeptical about your claim of getting 0%, or any other output. I don't think that stuff will even come close to compiling before fixing quite a few compiler errors. You are assigning Intger objects to integer values and all kinds of other silly and inconsistent things. Even after you do the necessary minimal edits to get a successful compile, it looks like you'll get NullPointerExceptions (and maybe ArithmeticException, too) before you get any results. So are you just bluffing about seeing some output, or did you post version 1.0 while you are compiling version 3.0?

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: String.trim method Posted: Jan 13, 2005 3:13 AM
Reply to this message Reply
Dear Matt Gerran,

You're right, after I did the necessary minimal edits to get a successful compile, I did get NullPointerExceptions
but not ArithmeticException and did get results: 0%.

However, I'm not bluffing about seeing some output,
and I'm still working on version 1.0.

I did start this coursework on the 12 December 2004, did work on all Christmas time...
If you can help me to have in output some better result, please do so.

Many thanks for your understanding.
CG.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: String.trim method Posted: Jan 14, 2005 2:03 AM
Reply to this message Reply
A compilation tip brother:

I don't know if you already do this, but if you
dislike using the debuggers in JBuilder (if that's
where you're working), you may also use println
statements to catch where your NullPointerExceptions
are occuring, in somecases where you loose accuracy.

E.g if I want to debugg figure out where I lose accuracy
or what the values are in the following code:

MyObject myObj = null;
Object2 ob2 = someObject();
ob2.someOperation();
obj2.operationOnMyObject(myObj);

This will read a null pointer Exception... To figure
out where the BreakDown is do the following:

System.out.println("Executing myObj init");
MyObject myObj = null;
System.out.println("myObj initialized");
obj2.someOperation();
System.out.println("obj2.someOperation executed");
obj2.operationOnMyObject(myObj);
System.out.println("Operation on myObj successful");

Look through your console, and trace which println
is not executed, and this usually gives you an
idea of what section of code to fix.

Remember to remove unnecessary println statements
when finished.

Believe me this debugging method is extremely helpfull,
I prefer it to using JBuilder's debugging methods...

Good luck.

Spike

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