/* StreamTokenizer and getting number-length example Copyright (C) 2009 Vegard Hammerseth (http://vegard.hammerseth.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. v1.0.0 */ import java.io.StreamTokenizer; import java.io.InputStreamReader; import java.io.BufferedReader; class numberDigits { public static void main(String[] args) throws Exception { double num = -1; StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); System.out.println("Write a number:"); while (true) { while (st.nextToken() != StreamTokenizer.TT_NUMBER) { System.out.println("Invalid number.\nWrite a number:"); continue; } num = st.nval; if (num < 0 || num > 999) { System.out.println("Number must be between 0 and 999.\nTry again:"); continue; } break; } /* convert int to string to find length */ String digit = String.valueOf(num); System.out.println(digit.length()-2 + " digit(s)."); } }