• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Carey Brown
  • Frits Walraven

CattleDrive question: Converting a string to an int

Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an effort to try understand a bit more about java, I started doing the cattledrive assignments on this website (no book), thankfully the help pages are less cryptic than the oracle tutorials. but I do have a couple of questions, if someone can spare 5 minutes to answer them.

Why convert a string to an int at all? why not just start with an int.

In my code below, I had a lot of trouble compiling, because String z wasn't initialized, after a little googling I came up with String z = "0"; I'm not certain that was the best way to get around that compiler error, please advise.

Finally I can detect even odd up to 9 digits eg - 123456789 = odd number, once it goes over 10 digits it crashes, if I used a long instead on int would it detect / work up to 20 digits, or is the long function something completely different?

Thanks for any replies.

P:S I just read I have to pay for feedback concerning cattledrive assignments. please ignore.
Posts: 3036
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to pay for private nitpicking of assignments, not generalized feedback.

In general, it is better to start with an int if you can, rather than convert a convert a String. In your example, there's no reason to create z, a String variable, set to "0", if all you're going to do is convert it to int. However, input doesn't always come from the program code. Sometimes you'll read user input, or read from a file, or a network. You've already seen that the Scanner helps you do this. When you call nextInt(), it is actually reading characters typed by the user, and then converting them (if possible) to an int, which you can use for arithmetic. The Scanner is doing the same work that Integer.parseInt() does, just in what can be a more convenient way for the programer. If you were to output the int, say with System.out.println(), it would have to be formatted back into a String. However, the println() method does that job for you.

Be careful with your loops. You now have a do-while loop as the only statement inside a while loop. You only should use a single loop here. There also doesn't seem to be any way to break out of the inner loop. Do you mean there to be a way to exit your program? What user input should trigger that exit?
lowercase baba
Posts: 12827
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the forma NITPICKING, there is a fee. but you can always post questions.

Why convert a String to an int? Because usually what you get from a user is a String. You can't be sure what they type will be a number, but anything they type can be a String. Further, sometimes these exercises ask you to do things just so you get a feel for what can be done.

Are you sure about the 10 digits? Have you tried 999999999? what about 1000000000?
Posts: 68857
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got two loops?
Why have you got any loops at all?
Why have you got the String z?
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
    Bookmark Topic Watch Topic
  • New Topic