for循环检查字符串是否包含符号?

时间:2015-11-21 16:34:00

标签: java

我正在开发一个程序,要求用户输入变量的名称,程序会检查它是否使用了好的样式,合法但风格很差,或者完全违法。出于此程序的目的,良好的样式意味着变量仅使用字母和数字,并且仅以小写字母开头。程序不必检查变量名中的第二个,第三个或等等单词是否以大写字母开头。我花了几天左右的时间试图弄清楚如何让for循环检查变量名中的每个字符,看它是否是符号,而我却无法做到。任何帮助将不胜感激,如果你正在投票这个问题,请告诉我为什么这样我可以在将来更好地提出我的问题:) 这是我目前的代码。它不编译;它说char不能被解除引用,但我不需要任何人为我重写代码。我只需要别人告诉我他们将如何尝试检查符号的变量名称。

import java.util.Scanner;

public class VariableNameChecker
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        String variableName;
        int count;
        char ch;
        String status = "good";

        System.out.print("This program checks the properness of a proposed Java variable name.");
        System.out.print("\nEnter a variable name (q to quit): ");
        variableName = sc.next();

        for(count = 0; count < variableName.length(); count++) //check each character of variableName to see if it is good style
        {
            ch = variableName.charAt(count);
            if (ch == ' ')
            {
                status = "bad";
            }
            else if (ch.isDigit || ch.isUpperCase || ch == '_')
            {
                status = "poor";
            }
        }
        if (status.equals("bad"))
        {
            System.out.print("Illegal.");
        }
        else if (status.equals("poor"))
        {
            System.out.print("Poor style");
        }
        else //variableName only contains letters and digits, and only begins with a lowercase letter
        {
            System.out.print("Good!");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用正则表达式:

  1. 从小写char ^ [a-z]
  2. 开始
  3. 仅字母数字[a-zA-Z0-9] *
  4. 所以,最后,你需要检查这个正则表达式:&#34; ^ [a-z] [a-zA-Z0-9] *&#34;

    import java.util.Scanner;
    
    public class VariableNameChecker {
    
    private static final String MATCH_REGEX = "^[a-z][a-zA-Z0-9]*";
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            String variableName;
            String status = "good";
    
            System.out.print("This program checks the properness of a proposed Java variable name.");
            System.out.print("\nEnter a variable name (q to quit): ");
            variableName = sc.next();
            if (!variableName.matches(MATCH_REGEX)) {
                status = "bad";
            }
            System.out.println(status);
        }
    }
    

答案 1 :(得分:0)

阅读Scanner的整行。否则,status会从所有空格中对您的输入进行标记。

使用也可以使用if-else变量进行最终打印。无需添加其他q语句。

您必须在无限循环中循环代码,以便提示更多值。如果您想退出,可以按照说明使用public static void main(String[] args) { Scanner sc = new Scanner(System.in); String variableName; String status; boolean firstTime; System.out.print("This program checks the properness of a proposed Java variable name."); while (true) { System.out.print("\nEnter a variable name (q to quit): "); status = "Good!"; variableName = sc.nextLine(); if (variableName.equals("q")) { System.exit(0); } firstTime = true; for (char ch : variableName.toCharArray()) //check each character of variableName to see if it is good style { if (Character.isJavaIdentifierPart(ch)) { if (Character.isLetterOrDigit(ch)) { if (firstTime) { if (Character.isDigit(ch)) { status = "Illegal"; break; } if (Character.isUpperCase(ch)) { status = "Poor Style"; break; } } } else { status = "Poor Style"; break; } } else { status = "Illegal"; break; } firstTime = false; } System.out.println(status); } }

我试了一个例子。它可能会有所帮助..

.Wait()
相关问题