i was looking at this code and thinking when is it a good idea to use the do-while method.
import java.io.*;
class ValidHex
{
public static void main(String[] args)
{
Console console=System.console();
String hex;
boolean valid;
do{
System.out.println("Please enter a valid hexadecimal number");
hex=console.readLine();
if(hex.length()==0)
valid=false;
else
valid=true;
for(int index=0;index<hex.length() && valid; index++)
{
switch(hex.charAt(index))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F':
break;
default:
valid=false;
break;
}
}
if(!valid)
System.out.println("Illegal number");
}while(!valid); // why do i need to use that do-while? what is it good for?
}
}