i would like to use the following constant:
final String ADD = "Add text";
But my CheckStyle tool tells me that 'ADD' does not match the pattern '^[a-z][a-zA-Z0-9]*$'.
Could anyone please tell me what is wrong with 'ADD'?
Means '^[a-z][a-zA-Z0-9]*$' that every name has to start with a low character?
Is there no other possibility?
Thanks for answers.
^[a-z][a-zA-Z0-9]*$
This regex describes something which starts with lowercase and the remainder is composed of uppercase, lowercase, and numbers. (Examples: aVariable, variable, aNewVariable, variable7, aNewVariable7.)
If you want your field to be constant and static, use:
static final String ADD = "Add text";
Otherwise, use:
final String add = "Add text";
If it is a constant you want, it should also be static
static final String ADD = "Add text";
Constants normally use uppercase letters, but since your variable was not static, it was not interpreted as a constant.
This Regex indicate the need for camelCase with the first letter being small and then every next word having the first letter in it as capital letter.
I just ran into the same problem, turns out it was because it is expected for the Java codebase I was working on to use camel case for all variables as the naming convention. So be sure to check if your variables are named according to the regex pattern ^[a-z]([a-z0-9][a-zA-Z0-9]*)?$. In my case, I got stuck in the Python mode and had my variable named version_regex instead of versionRegex. Once I have made the needed correction the error is no longer thrown.
Related
I am 16 and trying to learn Java, I have a paper that my uncle gave me that has things to do in Java. One of these things is too write and execute a program that will accept an extended message as a string such as
Each time she saw the painting, she was happy
and replace the word she with the word he.
Each time he saw the painting, he was happy.
This part is simple, but he wants me to be able to take any form of she and replace it we he like (she to he, She to He, she? to he?, she. to he., she' to he' and so on). Can someone help me make a program to accomplish this.
I have this
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Write Sentence");
String original = keyboard.nextLine();
String changeWord = "he";
String modified = original.replaceAll("she", changeWord);
System.out.println(modified);
}
If this isn't the right site to find answers like this, can you redirect me to a site that answers such questions?
The best way to do this is with regular expressions (regex). Regex allow you to match patterns or classes of words so you can deal with general cases. Consider the cases you have already listed:
(she to he, She to He, she? to he?, she. to he., she' to he' and so on)
What is common between these cases? Can you think of some general rule(s) that would apply to all such transformations?
But also consider some cases you haven't listed: for example, as you've written it now, your code will change the word "ashes" to "ahes" because "ashes" contains "she." A properly written regex expression allows you to avoid this.
Before delving into regex, try and express, in plain English, a rule or set of rules for what you want to replace and what it should be replaced with.
Then, learn some regex and attempt to apply those rules.
Lastly, try and write some tests (i.e. using JUnit) for various cases so you can see which cases your code is working for and which cases it isn't working for.
Once you have done this, if something still doesn't work, feel free to post a new question here showing us your code and explaining what doesn't work. We'll be happy to help.
I would recommend this regular expression to solve this. It seems you have to search and replace separately the uppercase S and the lowercase s
String modified = original
.replaceAll("(she)(\\W)", "he$2")
.replaceAll("(She)(\\W)", "He$2");
Explanation :
The pattern (she) will match the word she and store it as the first captured group of characters
The pattern (\\W) will match one non alphabetic character (e.g. ', .) and store it as the second captured group of characters
Both of these patterns must match consecutive parts of the input string for replaceAll to replace something.
"he$2" put in the resulting string the word he followed by the second captured group of characters (in our case the group has only one character)
The above means that the regular expression will match a pattern like She'll and replace with He'll, but it will not match a pattern like Sherlock because here She is followed by an alphabetic character r
I have a thought:(might too completed)
I wonder if I can substring(1) then use .toUpperCase() for the
string
and after that use getLength() to have the length then
.toLowerCase() the rest in the string
Is there a easier way to do this? So I can have the value stored in "Xxxx" format.
Thank you.
For example:
No matter user input the value as "hELlo" or "HEllO" , the system always store the value as "Hello". That's may explain my question.
I wrote a small utility for my self. It might help you. I was sure that my string would always have alphabets in it so did not care about any other characters. you might want to modify as per your needs.
int length = "yourstring".length();/// get the length of the string
String camelCase = removeCharacters.substring(0, 1).toUpperCase();// upper case the first alphabet
camelCase = camelCase + "yourstring".substring(1, length).toLowerCase();// lowercase all other alphabets
Substring has a one-arg version and a two-arg version. The second argument is non-inclusive. So, the first bit of the string you want is
myString.subString(0, 1)
and the second bit is
myString.subString(1)
You can then call the toUpper and toLower methods on the results, and concatenate them.
Yes, the Apache Commons-Lang project has a WordUtils class with a capitalize method, as in the following example:
WordUtils.capitalize("i am FINE") yields: "I Am FINE"
I found no way to replace only parts of a string with velocity.
Assume the following velocity template:
$test
something$test
$test.something
I want to replace all occurrences of $test with the string TEST.
I therefore use the following code:
VelocityContext context = new VelocityContext();
context.put("test", "TEST");
This is the result, I expect:
TEST
somethingTEST
TEST.something
But what I really get is:
TEST
somethingTEST
$test.something
So obviously Velocity doesn't replace a variable if there is some text after the variables name.
What can I do to replace a variable even if it is only a part of a string?
The $test.something is causing the problem.
It is expecting a variable something inside the object test.
Use ${test}.something instead...
--Cheers, Jay
The problem you face here is not 'obviously Velocity doesn't replace a variable if there is some text after the variables name'.
The symbol '$' is used to represent beginning of any line. So you have to find a way to escape that symbol in the input string so that the literal meaning of '$' is not considered
For some reason my multi delimiter split is not working. Hope it just a syntax error.
This works, but I want to also split if it finds end date
String dateList[] = test.split("(?="+StartDate+")");
But this does not. Am I missing something?
String dateList[] = text.split("[(?="+StartDate+")(?="+EndDate+")]");
You cannot use "lookarounds" in a custom character class - they'd be just interpreted as characters of the class (and may not even compile the pattern properly if a malformed range is detected, e.g. with dangling - characters).
Use the | operator to alternate between StartDate and EndDate.
Something like:
String dateList[] = text.split("(?="+StartDate+"|"+EndDate+")");
Notes
You also may want to invoke Pattern.quote on your start and end date values, in case they contain reserved characters.
Java variable naming convention is camelBack, not CamelCase
This is a question from a Java test I took at University
I. publicProtected
II. $_
III. _identi#ficador
I've. Protected
I'd say I, II, and I've are correct. What is the correct answer for this?
Source of the question in spanish: Teniendo la siguiente lista de identificadores de variables, ¿Cuál (es) es (son) válido (s)?
From the java documentation:
Variable names are case-sensitive. A variable's name can be any legal
identifier — an unlimited-length sequence of Unicode letters and
digits, beginning with a letter, the dollar sign "$", or the
underscore character "". The convention, however, is to always begin
your variable names with a letter, not "$" or "". Additionally, the
dollar sign character, by convention, is never used at all. You may
find some situations where auto-generated names will contain the
dollar sign, but your variable names should always avoid using it. A
similar convention exists for the underscore character; while it's
technically legal to begin your variable's name with "_", this
practice is discouraged. White space is not permitted. Subsequent
characters may be letters, digits, dollar signs, or underscore
characters. Conventions (and common sense) apply to this rule as well.
When choosing a name for your variables, use full words instead of
cryptic abbreviations. Doing so will make your code easier to read and
understand. In many cases it will also make your code
self-documenting; fields named cadence, speed, and gear, for example,
are much more intuitive than abbreviated versions, such as s, c, and
g. Also keep in mind that the name you choose must not be a keyword or
reserved word.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
In short: yes, you're right. You can use underscores, dollarsigns, and characters to start a variable name. After the first letter of the variable name, you can also use numbers. Note that using dollar signs is generally not good practice.
From your comment, you said that your teacher rejected "II". Under your question, II is perfectly fine (try it, it will run). However, if the question on your test asked which are "good" variable names, or which variable names follow common practice, then II would be eliminated as explained in the quotation above. One reason for this is that dollar signs do not make readable variable names; they're included because internally Java makes variables that use the dollar sign.
What is the meaning of $ in a variable name?
As pointed out in the comments, IV is not a good name either, since the lower case version "protected" is a reserved keyword. With syntax highlighting, you probably wouldn't get the two confused, but using keyword-variations as variable names is certainly one way to confuse future readers
Private protected public are reserved or keywords in java.. Use _ or to use that those words.. example
int public_x;
int protected_x;
String private_s;