• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    that my friend has made this click for me ! thank you very much.
     
    lowercase baba
    Posts: 13081
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 789
    Python C++ Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Anderson gave himself the promotion. So I gave myself this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic