Question

Write a Java program that checks the properness of a given variable name. More specifically, your...

Write a Java 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 (no spaces allowed, must begin with a letter)
  2. legal, but uses poor style (should only use letters or digits)
  3. good

You don’t need to check for an uppercase letter for the first letter in the second word, third word, etc.

Sample session:

This program checks the properness of a proposed Java variable name.
Enter a variable name (q to quit): streetAddress2
Good!
Enter a variable name (q to quit): street address2
Illegal.
Enter a variable name (q to quit): StreetAddress2
Good!
Enter a variable name (q to quit): 2ndStreetAddress
Illegal.
Enter a variable name (q to quit): street$address$2
Legal, but uses poor style.
Enter a variable name (q to quit): q
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP

nikhil@nikhil-Vostro-15-3568: -/Desktop/CODE File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$

import java.util.*;
class A{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("This program checks the properness of a proposed Java variable name.");
       while(true){
           System.out.print("Enter a variable name (q to quit): ");
           String s = sc.nextLine();
           if(s.equals("q"))
               break;
           if(s.contains(" ") || (s.charAt(0)>='0' && s.charAt(0)<='9'))
           {
               System.out.println("Illegal");
               continue;
           }
           int flag=0;
           for(int i=0;i<s.length();i++){
               if(((s.charAt(i)>='a' && s.charAt(i)<='z') || (s.charAt(i)>='A' && s.charAt(i)<='Z') || (s.charAt(i)>='0' && s.charAt(i)<='9'))==false)
               {                  
                   System.out.println(" Legal, but uses poor style");
                   flag=1;
                   break;
               }
           }
           if(flag==1)
               continue;
           System.out.println("Good!");
       }
   }
}

1 2 3 5 6 7 8 import java.util.; class A{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); Syste

Add a comment
Know the answer?
Add Answer to:
Write a Java program that checks the properness of a given variable name. More specifically, your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • – Palindrome Game Please write a Java program to verify whether a given word is a...

    – Palindrome Game Please write a Java program to verify whether a given word is a palindrome. You must stop your program when the user enters ‘@’ character as the word. You may write a recursive program to solve this game, but you don’t have to. You may use a stack and/or a queue to solve this game, but you don’t have to. You must run 3 test cases for your program.   Your test case #1 must look as follows:...

  • Your task for this project is to write a parser for a customer form. You need to develop a Java a...

       Your task for this project is to write a parser for a customer form. You need to develop a Java application using Parboiled library. Your program should include a grammar for the parser and display the parse tree with no errors. Remember that your fifth and sixth assignments are very similar to this project and you can benefit from them. The customer form should include the following structure: First name, middle name (optional), last name Street address, city, state (or...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Write a Java program to do the following with your name. This can all be done...

    Write a Java program to do the following with your name. This can all be done in the main() method. 1. Create a String variable called myName and assign your personal name to it. Use proper capitalization for a legal name. I.e. String myName = "Billy Bob"; 2. Load myName with the upper case version of itself and display the result. 3. Load myName with the lower case version of itself and display the result. 4. Capitalize the first letter...

  • (Java) HELP! Create a program that will ask the user to enter their first name and...

    (Java) HELP! Create a program that will ask the user to enter their first name and their favorite number. Ask the user if they would like the information repeated back to them. If they type "Y" display the answers back to them. If they type anything else, display the word "Goodbye". An example of what your output window should look like is below. The system-generated output is in red. The user-provided input is in green. (Your actual program will only...

  • Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is...

    Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is as follows: InputOrGenerateDiceRolls program should ask the user whether the user prefers 1) to roll his/her own dice, 2) use computer-generated dice rolls, or 3) quit the program. If the user prefers to roll his/her own dice, the program should prompt the user to enter 5 digits in the range from 1-6 separated by spaces in any order with duplicates allowed. However, if the...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Write a program in C plus plus that reads input a word at a time until...

    Write a program in C plus plus that reads input a word at a time until a lone q is entered.The program should then report the number of words that began with vowels, the num- ber that began with consonants,and the number that fit neither of those categories. One approach is to use isalpha() to discriminate between words beginning with letters and those that don’t and then use an if or switch statement to further iden- tify those passing the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT