Need java class help!

ZXQ

Limp Gawd
Joined
Jul 22, 2004
Messages
160
I don't normally ask for help with homework, but my current Java professor actually encourages students to seek help, because as in a corporate environment, you aren't working alone.

Anyway, here's the problem text:

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:
Only use letters and digits.
Use a lowercase letter for the first character.

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
Legal, but uses poor style.
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
]

now, I don't know why, but the solution to this is eluding me to a point of insanity!

This is what I have, but it doesn't work!

Code:
/****************************************************
* NameChecker.java
*
*
* Name Checker Project, checking for legality
*****************************************************/

import java.util.Scanner;

public class NameChecker
{
  public static void main(String[] args)
  {
	  Scanner stdIn = new Scanner(System.in);
	  
	  String userInput;
	  char inputExtract;
	  char loopExtract;
	  boolean firstLetter = true;
	  boolean style = true;
	  int i;
	  	  
	  System.out.print("Enter a variable name (q to quit): ");
	  userInput = stdIn.nextLine();
	  inputExtract = userInput.charAt(0);
	  
	  if (!(Character.isLetter(inputExtract)))
	  {
		  firstLetter = false;
	  }
		  
	  for (i = 1; 1 < userInput.length() && firstLetter; i++)
	  {
		  loopExtract = userInput.charAt(i);

			if (!(Character.isLetterOrDigit(loopExtract) || loopExtract == '$' || loopExtract == '_'))
				{
					style = false;
				}
			
	  }

	  if (firstLetter && style)
	  {
		  System.out.println("Good!");
	  } 
	  else if (firstLetter && !(style))
	  {
		  System.out.println("Legal, but uses poor style.");
	  }
	  else
	  {
		  System.out.println("Illegal.");
	  }
	}
} // end class
 
you should be checking for validity and style separately (i.e. use 2 if statements) (and don't forget to validate the first character as well since it's outside of your for loop). if you find an invalid character, you can set a boolean and break out of the for loop since only one character needs to be invalid for the whole thing to be invalid.
 
You'll need a few checks. I'll list them in order that I think is best.

1. Check for legality of the variable, meaning if it has a space separating two words, it's illegal. This can be easily checked by splitting the string by " " character, if the output has two strings, the variable name is not valid. Just do boolean check for userInput.Contains(" "); (should work, havent tested it if my memory serves me right " " would be equivalent to CharSequence object);

2. Check for the first character for being a letter (you're doing this already) then check if it's in lowercase. isLowerCase works good here.



Your solution is almost correct. You just need to add the check for first characters case and add a check for space inside the variable (The way you have it now, it will flag it as poor style, but not illegal). Oh yeah and take note of what guy above me said, he's right.
 
you should be checking for validity and style separately (i.e. use 2 if statements) (and don't forget to validate the first character as well since it's outside of your for loop). if you find an invalid character, you can set a boolean and break out of the for loop since only one character needs to be invalid for the whole thing to be invalid.

So, by what you're saying, if i just have a simple legality check in the loop, wait! I GOT IT.

Ill post code in a couple minutes.
 
You'll need a few checks. I'll list them in order that I think is best.

1. Check for legality of the variable, meaning if it has a space separating two words, it's illegal. This can be easily checked by splitting the string by " " character, if the output has two strings, the variable name is not valid. Just do boolean check for userInput.Contains(" "); (should work, havent tested it if my memory serves me right " " would be equivalent to CharSequence object);

2. Check for the first character for being a letter (you're doing this already) then check if it's in lowercase. isLowerCase works good here.



Your solution is almost correct. You just need to add the check for first characters case and add a check for space inside the variable (The way you have it now, it will flag it as poor style, but not illegal). Oh yeah and take note of what guy above me said, he's right.

I took #1 literately. Thanks man!
 
BAM! Try and break it?

Code:
/****************************************************
* NameChecker.java
*
*
* Name Checker Project, checking for legality
*****************************************************/

import java.util.Scanner;

public class NameChecker
{
  public static void main(String[] args)
  {
	  Scanner stdIn = new Scanner(System.in);
	  
	  String userInput;
	  char inputExtract;
	  char loopExtract;
	  boolean legal = true;
	  boolean style = true;
	  int i;
	  	  
	  System.out.print("Enter a variable name (q to quit): ");
	  userInput = stdIn.nextLine();
	  inputExtract = userInput.charAt(0);
	  
	  while (!(inputExtract == 'q'))
	  {
		  if (!(Character.isLetter(inputExtract)))
		  {
			  legal = false;
		  }
	  
		  if (userInput.contains(" "))
		  {
			  legal = false;
		  }
	  
		  if (!(Character.isLowerCase(inputExtract)))
		  {
			  style = false;
		  }
	  
	  
		  for (i = 1; i < userInput.length() && legal; i++)
		  {
			  loopExtract = userInput.charAt(i);
		  
			  if (!(Character.isLetterOrDigit(loopExtract)))
					{
						style = false;
					}
			
		  }

		  if (legal && style)
		  {
			  System.out.println("Good!");
		  } 
		  else if (legal && !(style))
		  {
			  System.out.println("Legal, but uses poor style.");
		  }
		  else
		  {
			  System.out.println("Illegal.");
		  }
		  
		  System.out.print("Enter a variable name (q to quit): ");
		  userInput = stdIn.nextLine();
		  inputExtract = userInput.charAt(0);
	  }
  }
} // end class
 
if someone just presses enter without typing anything userInput will be "" and when you do charAt(0) you get an exception.

depending on the definition of legal (i'll assume java's definiton) the first character doesn't have to be a letter, it could be _ or $, for example. it just cannot be a number.

also, when i was talking about a legality check in the loop, i was referring to the inner for loop
for (i = 1; i < userInput.length() && legal; i++)

you need to check every character for legality. i wouldn't bother with contains(" ") if you do what i suggest because it would be found anyway.

so valid for the first char (ignoring case) is a-z _ or $. for the rest of the chars its a-z 0-9 _ or $. after seeing if it's valid, you can check if it's good style (which i think you've gotten already).
 
Also, before replace userInput.Contains(" "); with userInput.trim().Contains(" ") depending on whether your prof. cares if a variable starts/ends with spaces.
 
Also, before replace userInput.Contains(" "); with userInput.trim().Contains(" ") depending on whether your prof. cares if a variable starts/ends with spaces.

whats the functional difference betweek .trim().Contains() and just .Contains() ?
 
whats the functional difference betweek .trim().Contains() and just .Contains() ?

.trim() will remove any leading or trailing white spaces from the string.

" input".Contains(" ") would return true.
 
Back
Top