This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain to me why you would need to convert a sting to int  RSS feed

 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys i am starting out in programming and i am looking at example codes to help me understand. I have come across this code to learn inputing strings to arrays but there is (parseint) i have read into it and do not understand why you would need no convert a string to int. if someone could open my eyes to this it would be much appreciated.

here is the example i am looking at...

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to UseCodeTags (that's a link) when you post code. [Edit] And indentation. I added both for you this time.

This line

could be

Is that what you mean? If so, there I don't think there is a good reason.
 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didnt know you could put code in that way. um i am just looking at example code are you saying that would be an easy way to do it? Why do you think they have done it in that way using parseint?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use Integer#parseInt much less than you used to now Scanner is available and it will do it for you.

But you will have all sorts of things which are text, e.g. files, keyboard input, needing converting to numbers. You cannot do arithmetic with text.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To provide a little insight into why such a method is available follows:

  • Data representation in an electronic computing system is solely based on interpretation...

  • All a computer does is store and manipulate sequences of (for the sake of simplicity) patterns...

    It is up to the user to decide what the pattern represents...

    Now using established conventions, we have the following:

    0011_0000
    <- bit pattern to represent a character '0'
    0000_0000 <- bit pattern to represent the numeric value 0

    From this we can see that those patterns are completely different, but from a high level of viewing information on a screen, one can be misguided as viewing the character '0' and the numeric value 0 as being the same when the computer understands differently...

    So in order to perform correct manipulations on our data, an environment provides facilities for making the representations transparent to the user...

    Going back to your specific question, a String is simply a sequence of characters which will have different patterns from an actual numeric value even though the String may appear to represent a numeric value...

    In order to perform numeric operations on such a data type you must convert it into a representation that the computer can understand to perform such an operation, hence the reason for Integer.parseInt()
     
    mitchell cooper
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    that my friend has made this click for me ! thank you very much.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Remember, too, that languages evolve. The Scanner class has only been around since 1.5. Prior to that, i'm not sure what the best way to do this was, but I imagine parseint might have been (just about) your only option.

    Then someone said "hey, we do <this> a lot...is there some way we could simplify it by adding a new class to the language?".
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Before Scanner there were a few alternatives to parseInt, e.g. new Integer("123").intValue()
    But Integer#parseInt was probably the standard way to turn a String into an int.
     
    Guillermo Ishi
    Ranch Hand
    Posts: 789
    C++ Linux Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell cooper wrote: do not understand why you would need no convert a string to int. if someone could open my eyes to this it would be much appreciated.

    It probably originates back there when you had either text files or binary files. Text files are usually easier to deal with. In no small part because you could view them or modify them with a text editor. If you're reading in a number stored as ascii text then you have to convert that text string to a numeric type before you can do any operations with it. Text files also gave you a carriage return line feed as a convenient delimiter.
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guillermo Ishi wrote: . . when you had either text files or binary files. . . .
    Had? We still have binary files and text files.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!