The Artima Developer Community
Sponsored Link

Java Answers Forum
How to truncate a string, and read parts of it in as integer?

3 replies on 1 page. Most recent reply: Aug 21, 2003 2:01 AM by David

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
Dor

Posts: 13
Nickname: eis
Registered: Jul, 2003

How to truncate a string, and read parts of it in as integer? Posted: Aug 18, 2003 4:42 AM
Reply to this message Reply
Advertisement
Hello!

I am given some string formatted like this, "8k","256k"... Now I want to turn them into integers, say, 8192(=8*1024),...

Is there something to enable reading in all except the last 'k', and in the format of integer?

I know I can get it done by reading in every element, but am just lazy.

Thanks!


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: How to truncate a string, and read parts of it in as integer? Posted: Aug 18, 2003 8:02 AM
Reply to this message Reply
Lazy bugger... ;-)
public class Test {
        public static void main(String[] args) {
                String s = "8k";
                String strNumericPart = s.substring(0, s.length() - 1);
                int numericPart = Integer.parseInt(strNumericPart);
                int size = numericPart * 1024;
                System.out.println(size);
        }
}

Matthew Son

Posts: 2
Nickname: ms1
Registered: Aug, 2003

Re: How to truncate a string, and read parts of it in as integer? Posted: Aug 20, 2003 6:44 AM
Reply to this message Reply
I think the Integer.parseInt() should be surrounded by a try catch - just in case the data is not formatted as expected, otherwise you get a uncaught run time exception.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: How to truncate a string, and read parts of it in as integer? Posted: Aug 21, 2003 2:01 AM
Reply to this message Reply
"run time exception"

Are there any other kind? ;-)

Flat View: This topic has 3 replies on 1 page
Topic: basic help - getting 2 inputs Previous Topic   Next Topic Topic: How to get type from the input?

Sponsored Links



Google
  Web Artima.com   

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