The Artima Developer Community
Sponsored Link

Java Answers Forum
I am just in Java.. Please help

3 replies on 1 page. Most recent reply: Apr 5, 2002 8:29 PM by Racheal

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 3 replies on 1 page
Racheal

Posts: 2
Nickname: racoon
Registered: Apr, 2002

I am just in Java.. Please help Posted: Apr 1, 2002 8:20 PM
Reply to this message Reply
Advertisement
Hi

I need to know how this is possible, here is my problem.

I have a text say Gen123esy456swo789rld.
All i have to do is separate the text and the numerals then make the string with the text, and then place the numbers into an Integer array and then provide the sum. The input should be taken from the user at runtime.

I have tried doing this, but i am failing if i get multiple occurances of the number in the string.

Please help me with this, if anyone can implement this for me and give me the code i would be thankful to you.

Racoon


Matthew

Posts: 1
Nickname: letaylom
Registered: Apr, 2002

Re: I am just in Java.. Please help Posted: Apr 1, 2002 11:32 PM
Reply to this message Reply
Hi Racheal,

Take a look at the class StringTokenizer. Assign the whoel text input to a String (type) and use the tokenizer to iterate through each 'token' of it. You must then have a loop determine if the token is a letter or number, if its a number add it to your int array, if its a letter add to a StringBuffer. You will be left with an array and a buffer. from the array pass through each element and add the ints together, the string buffer can be closed and the new string value saved/printed etc...

Hope this helps.
Have a go at breaking down the problem like I did and tackling each part separately.

T

steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: I am just in Java.. Please help Posted: Apr 4, 2002 10:33 PM
Reply to this message Reply
The problem has a few things in it...
1 separate alpa and numeric characters,
2 parse numeric characters into numbers
3 add numbers.
4 concatenate the alpha characters.


toolbox:
1
java.lang.String.charAt() and or java.lang.String.substring()
boolean isnumber = java.lang.Character.isDigit(char c);
or
if("0123456789".indexOf(String singleStringCharacter)>0) itsadigit ;
String.length()


2
int k= Integer.parseInt(String digsequence);

3
sum+=k;

4
java.lang.String.concat(anotherString); or simply StringA+=stringb;

Aditionally, you have to figure out whether to insert " " spaces bewteen characters,
and if you want to add single digit numbers, or let the size of the numbers be determined by the character separation.

The basic approach:
Two Strings, one to make into a sentence, and one to parse into a number for the sum.
one number for the sum.

Either a for loop, or a while loop to run through the characters of the string.
read the loop one character at a time,
if it's a character
add it to the sentence string.
if(the char was a digit )parse the digit string and add it to the sum.
set the parse string to "";
else if its a digit
append it to the parseing string.
if the last char was alpha add a " " to the alpha.


This explaination is longer than the actual code would be, coding is often more efficient that way, but I think this should help.

Racheal

Posts: 2
Nickname: racoon
Registered: Apr, 2002

Re: I am just in Java.. Please help Posted: Apr 5, 2002 8:29 PM
Reply to this message Reply
Hi All

Thanks for the help i will try them out and get back to you.

Thanks again
Racoon

Flat View: This topic has 3 replies on 1 page
Topic: How to skip 404/401/403 Previous Topic   Next Topic Topic: JAVA implementation For CORBA

Sponsored Links



Google
  Web Artima.com   

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