简体   繁体   English

需要帮助检查 Java 中 If/else 语句中的空格

[英]Need help checking for white-space in If/else statements in Java

I am new to learning java and I'm doing a project for my online class and currently stuck on part of it.我是学习 Java 的新手,我正在为我的在线课程做一个项目,目前坚持其中的一部分。

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:更具体地说,您的程序应指定用户输入的变量名称是否为:

  • illegal (no spaces allowed, must begin with a letter)非法(不允许有空格,必须以字母开头)
  • legal, but uses poor style (should only use letters or digits)合法,但使用糟糕的风格(应该只使用字母或数字)
  • good好的

You don't need to check for an uppercase letter for the first letter in the second word, third word, etc.您不需要检查第二个单词、第三个单词等中第一个字母的大写字母。

So my problem is that since having space in a variable name is illegal, I need to check the user's input for space, and if there is one it needs to print that it is illegal.所以我的问题是,由于变量名中有空格是非法的,我需要检查用户输入的空格,如果有,则需要打印它是非法的。 I also need to check for special symbols (like $%#) and if it is anywhere but the first character, have it print that it is legal but improper.我还需要检查特殊符号(如 $%#),如果它不是第一个字符以外的任何地方,让它打印它是合法但不正确的。

I feel like this is super simple I just can't figure it out.我觉得这太简单了,我就是想不通。

import java.util.Scanner;
public class IdentiferCheck
{
   public static void main (String[] args)
{
   Scanner in = new Scanner (System.in);
  
  String variableName = "";
  char ch = ' '; //temp holder
  
  //Get User Input
  System.out.println("This program checks the properness of a proposed Java variable name.");
  System.out.println("Please enter a variable name (q to quit):");
  variableName = in.nextLine();
  
  
  //Check if variable name is proper
  do
  {
     //Check if first char is lowercase
     ch = variableName.charAt(0);
     
     if (Character.isLetter(ch) && Character.isLowerCase(ch))
     {
     
        System.out.println("Good!");
     
     }
     
     else if (Character.isDigit(ch) && Character.isUpperCase(ch) && variableName.contains(" "))
     {
     
        System.out.println("Illegal!");
     
     }
     
     
     //Allow user to input another name or quit
     System.out.println("Enter another name or press q to quit: ");
     variableName = in.nextLine();
     
  } while (!variableName.equalsIgnoreCase("q"));
}

}

make sure to read the following documentation for your task.请确保为您的任务阅读以下文档。 It's worth noting that the requirements don't actually match the real world requirements of Java variable names.值得注意的是,这些要求实际上并不符合 Java 变量名称的实际要求。 However, the most difficult part of this task is the logic rather than the script.然而,这项任务最困难的部分是逻辑而不是脚本。 I've written an example script for you below:我在下面为您编写了一个示例脚本:

Subsequent characters may be letters, digits, dollar signs, or underscore characters.后续字符可能是字母、数字、美元符号或下划线字符。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

import java.util.Scanner;

public class identifierCheck
{
public static void main(String[] args) {
    
    String input = " ";
    System.out.printf("type q to exit...\n");

    while (input.charAt(0) != 'q' && input.length() == 1) // Note the length so that you can use q for starting variable names
    {
        input = requestLine();

        if (checkValid(input))
        {
            System.out.printf("%s is legal...\n", input);
        }
    }
}

public static String requestLine()
{
    Scanner cmd = new Scanner(System.in);
    System.out.printf("Enter a variable name > ");
    return cmd.nextLine();
}

public static boolean startsWithLetter(String input)
{
    if (123 > (int)input.toLowerCase().charAt(0) && (int)input.toLowerCase().charAt(0) > 60)
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean containsInvalid(String input)
{
    if ((input.indexOf('$') != -1 || input.indexOf('_') != -1) && input.indexOf(' ') == -1)
    {
        System.out.printf("Using $ and/or _ in variable names is poor style...\n");
    }
    if (input.indexOf(' ') != -1) // Has a space in the string
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static boolean checkValid(String input)
{
    if (!startsWithLetter(input))
    {
        System.out.printf("%s is illegal, must start with a letter (note: $ and _ are 'valid' to start variable names)...\n", input);
        return false;
    }
    else if (containsInvalid(input))
    {
        System.out.printf("%s is illegal, it must not contain spaces...\n", input);
        return false;
    }
    else
    {
        return true;
    }
}
}

Good luck with your course!祝你课程顺利! Java's an excellent language to learn. Java 是一门极好的学​​习语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM