This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to begin!

 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I have to make a program that allows user input. I know that I will read from the console and reference a string object.For example I will use;
String a= Stdin.readln(); // where a will reference the string object. I am fine with this part. My program has to decide whether a string of characters or numbers is an integer or not. If it is a integer, It will print out this is an integer.If it isn't an integer it will pring Its not an integer. An integer must meet the following critera: numbers and no spaces and it could be a negative sign preceding numbers. If it is not an integer is must meet the following critera: characters not numbers and no spaces between them and numbers with spaces.
With this in mind I realize I can accomplish this task with an if statement and else if statements. BUt I am unsure how the charAt() String method will come into play. If anyone could point me to a start I would be greatfull. Thanks again...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it's not part of the assignment to do this the 'long' way, the Integer class
has a method to handle the requirements - you just have to wrap it in a try/catch block.
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like time for a loop of some kind with code that will look at all the characters in a String. Use an int as index into the chars of the String using the charAt(int) method to obtain the individual characters so they can be tested as per the assignment. The loop should end after you look at the last character.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic