• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to Integer Conversion.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hi fellas!

I'm new in java programming and I really want to learn how to write a code or just simply to create any kinds of program.
The attached code is simply ask you a name of any product or good and it's quantity and also the price of each item and after that it will calculate it and show you how much the total of that particular items. But every time I run the code it gives me a wrong calculation, obviously because of the "hashcode" but the problem is I don't know the exact code to put to convert the string into an integer.

Please I need a help....

Thank you,
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on earth are you using hashCode()? Do you know what hashCode does? It does not convert a String "12345" to the int 12345.
Why are you using regular expressions alongside Scanner? Scanner uses regular expressions itself, so let the Scanner do the hard work:-
int i = myScanner.nextInt();
Now, as Rob Spoor taught me a long time ago, the Scanner has hasNextXXX methods which also use regular expressions. So you can use them to confirm correctness incorrectness of the input, and you can use the next method to throw away the incorrect input:-Of course you cannot write that sort of code inside your current class. That requires a separate method inside a separate utility class which you can use for ever.
You cannot simply write myScanner.nextLine() after that, for reasons explained here. When you get a line via nextLine() or a token via next(), you may have to use a regular expression because there is no restriction to what they contain beyond not containing line ends or the delimiter respectively. Similarly you need the nextLine call wrapped in a method in your utility class.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Haider Mezta wrote:The attached code is simply ask you a name of any product or good and it's quantity and also the price of each item and after that it will calculate it and show you how much the total of that particular items. But every time I run the code it gives me a wrong calculation, obviously because of the "hashcode" but the problem is I don't know the exact code to put to convert the string into an integer.


OK, then what you need to do is isolate the problem.

Right now, you have 77 lines of code that is doing all sorts of things, only ONE of which (at least as far as you know) is not working.

So, my advice: Set that program aside for now and write a new class called NumberEntry. That class should do ONE thing, and one thing only:
  • Read a number from the keyboard.

  • You'll probably also want to print it out to make sure that it read it correctly, and you'll want to test it with lots of different values, including invalid ones like "fred".

    And don't even think about writing another line of code in this program until your NumberEntry class works. Every single time, and with any input.
    And when it does, it should then be a simple matter to transfer that (working) code back to your Counter class.

    Basically, you've written far too much code without testing it properly. Don't worry, it's a common mistake and we've all done it (I still do sometimes ).

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:. . . And don't even think about writing another line of code in this program until your NumberEntry class works. Every single time, and with any input. . . .

    That includes incorrect input.

    I suggest you change what I showed earlier by adding an error message. Changeto……or similar.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic