Question
Beginner Java Program- boolean, do/while loops, for loops, if and if/else loops-Please help!!! Write a program that checks the properness of a given variable name.
Beginner Java Program- boolean, do/while loops, for loops, if and if/else loops-Please help!!!
Write a program that checks the properness of a given variable name. More specifically, your program should specify whether a user-entered variable name is:
(1) illegal,
(2) legal, but uses poor style, or
(3) good.
There are different opinions as to what constitutes good style for a variable name. For this program, check for good style using these rules:
(1)Only use letters and digits.
(2)Use a lowercase letter for the first character.
You dont need to check for an uppercase letter for the first letter in the second word, third word, etc.
My Program So Far :
import java.util.Scanner;
public class Lab08
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String variableName;
System.out.println("Enter a variable name:");
variableName=input.next();
for(int count = 0; count < variableName.length(); count++)
{
if(!(Character.isLetterOrDigit(variableName.charAt(count))))
{
System.out.print("Illegal.");
}
else if(Character.isUpperCase(variableName.charAt(0)))
{
System.out.println("Legal, but uses poor style.");
}
else if(Character.isDigit(variableName.charAt(0)))
{
System.out.println("Legal, but uses poor style.");
}
else if(Character.isLowerCase(variableName.charAt(0)))
{
System.out.println("Good.");
}
}
input.close();// closes the scanner
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started