Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

manipulating a string

Thread view: 
willerubrneck - 10 Mar 2005 14:13 GMT
newbie question
what i'm trying to do is manipulate a string from a GUI textfield. I want to
be able to break apart a string, then convert each character to a single
integer, so that I can pass each integer through a loop and use it in a
calculation.
I'm trying to convert numbers from one base to another, just bases 1 - 10. I
believe there is an easier way to change these numbers, but this is my
assignment.

example:
if user types in 2345
I want to first use the 2, then 3, then 4, next 5 in this order
I tried something like digit =
Integer.parseInt(enterNumber.getText().charAt(i));
i is a looping condition.

i get errors like "can't resolve symbol "  i've declared digit as an
Integer.
i've also got errors "int cannot be dereferenced" when I tried some aother
things

any suggestions would be appreciated
Daniel Tahin - 10 Mar 2005 16:01 GMT
Hi!

> newbie question
> what i'm trying to do is manipulate a string from a GUI textfield. I want to
[quoted text clipped - 14 lines]
> i get errors like "can't resolve symbol "  i've declared digit as an
> Integer.

You get these errors, because parseInt() requires a String as argument,
but charAt(i) returns a char. And the other problem is, that digit must
bedeclared as an int, because parseInt() returns an int.
But back to your problem.
You can solve it, by casting the return value of charAt(i) to int, like:
   int digit = (int) charAt(i);  But i don't know, whether this is the
preferred way, because the String will be converted to a character and
not to a byte or int...
The other way: int digit = Integer.parseInt(
enterNumber.getText().substring(0, 1)  );
substring(0, 1) returns the String, that contains only "2",
substring(1, 2) will contain "3" ...

> i've also got errors "int cannot be dereferenced" when I tried some aother
> things
>
> any suggestions would be appreciated

Hope this helps...
willerubrneck - 10 Mar 2005 20:30 GMT
This seems to work but once I get to the end of string I get an "end of
index" error.
this what i'm trying to accomplish.
ex. 2010 base 3 = 57,

firstBase = Integer.parseInt(firstBaseTF.getText());
// equals 3
count =(enterNumberTF.getText().length());
// equals 4
do
{
digit = Integer.parseInt(enterNumberTF.getText().substring(i, i + 1));
// gets digit from string
newNumber = newNumber + (digit * (firstBase ^ (count - i)));             //
converts number to base specifed
i++;
}
while(i <= count);

> Hi!
>
[quoted text clipped - 36 lines]
>
> Hope this helps...
Gordon Beaton - 11 Mar 2005 07:17 GMT
> this what i'm trying to accomplish.
> ex. 2010 base 3 = 57,
>
> firstBase = Integer.parseInt(firstBaseTF.getText());

First, realize that the base is simply a textual representation of the
given value. The value itself doesn't change, you don't a new int to
store a number in a different base.

Here's an extremely simple way to display the value in base 3:

 System.out.println(Integer.toString(firstBase, 3));

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

willerubrneck - 11 Mar 2005 12:54 GMT
I got it to accomplish what I was trying to do, thanks everyone.

j = (count - 1);
do
{

digit = Integer.parseInt(enterNumberTF.getText().substring(i, i + 1));
newNumber = newNumber + (digit * (Math.pow(firstBase, j)));
i++;
j--;
}
while(i < count);

>> this what i'm trying to accomplish.
>> ex. 2010 base 3 = 57,
[quoted text clipped - 10 lines]
>
> /gordon


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.