Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A problem determining the length of an int variable converted to a string..........

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a program whereby I'm accepting input form the keyboard, converting it to a String, and then displaying it.
Firstly however, I want to check whether the inputted value comprises more than 1 charcater or not.
I've done the following :-

In other words, if I enter the value '22' at the keyboard, because it comprises of more than one digit, i want the program to print "two digits present!".
But it doesn't work - nothing happens.
What am I doing wrong here?
[ December 23, 2002: Message edited by: Marilyn de Queiroz ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args is an array with all command line arguments (however many there are). args[0] is the String representing the first argument (if there is one). So you want to look at args[0].length() rather than args.length. Of course, make sure args.length > 0 before trying to access args[0].
 
Thana Bujakorn
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are differences b/w: length and length(), i modified your prog, hope it will help you understand better.

[ December 23, 2002: Message edited by: Marilyn de Queiroz ]
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one of several possible ways of getting a count of the digits in an integer.

[ December 22, 2002: Message edited by: Michael Matola ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Cattle Drive...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic