Octal representation of characters in java - java

I write the code System.out.println('\577'); and it produces unclosed character literal error. What's the problem here as all the digits are in the limits of the octal integers?

Just change it to System.out.println("/577");. You are getting error because you are using ' ' for a String but single quot (' ') can't take more that a Character, So you need to use " " (double quot) when you take into more than one character or string. Hope it works.

Related

How to convert any kind of white space to a char?

I use String.strip() (Java 11) to remove trailing & leading white spaces from a String. There are 25 different kinds of white spaces in a String. I want to test my code with some of these 25 types of white space.
I have a code example which converts a particular type of white space (ex. \u2002) into a char and then uses it in a String. When I try to convert another white space type like \u000A to char, I get a compiler error. Why does this happen and how to fix it ?
public static void main(String...args){
char chr = '\u2002';//No problem.
//Compiler error :
//Intellij IDEA compiler - Illegal escape character in character literal.
//Java compiler - java: illegal line end in character literal.
chr = '\u000a';
String text = chr + "hello world" + chr;
text = text.strip();
System.out.println(text);
}
Are you sure you're not seeing this error instead?
error: illegal line end in character literal
Escape sequences like \u000a are processed very early in the compilation process. The \u000a is being replaced with an actual line feed character (code point 10).
It's as if you wrote this:
chr = '
';
which is why, when I try and compile your code using JDK 11.0.8, I get the "illegal line end" error.
This early conversion is described in the Java Language Specification:
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (§3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.

Use "[", " ]" symbols in regex in java

I wanted to know how to use "[", "]" symbols in regex, I am getting compile time error. Could anyone help me?
I have to split a string which holds "[", "]" symbols in java, but getting compile time error
String strng = "[11 11] 2000";
String ready[] = strng.split("[] ");
Compile time error : Unclosed character class;
You need to escape those characters by using \[\] because in regex [] are used to create a range.
E.g. [A-Z] are all characters from A to Z in uppercase.
Or [ABC] is checking if your string contains one of the three characters A, B or C.
So in Java use this code to split your string:
strng.split("\\[\\] ");
Your regex needs to be structured as a character class containing characters '[', ']', and ' '. Since character class syntax includes square brackets, the brackets inside the character class need to be escaped with slashes. Finally, since escape sequences are used as part of a Java string, you need to escape each backslash, like this:
String ready[] = strng.split("[\\[\\] ]");
String strng = "[11 11] 2000";
String ready[] = strng.split("\\[|\\]");
System.out.println("ready = " + ready[1]);
ready = 11 11
You need to escape the special characters by escape characters.
so [ has to be represented as \[ and a ] has to be represented as \].
In Java, you need to add \\ to represent a \.
So effectively \\[ and \\] are used to avoid the special meaning associated with [] and use them as a normal character.

regex for single space and/or apostrophe in java

I'm having issues with my regex.Could anyone please help me on this? Requirement: String should be alphabetic and can include single apostrophe and or single space(size should be minimum of 2)
Valid strings:
1. 'abc
2.' abc
3.abc '
4.abc'
5.a 'bc
6.a' bc
I have used the below regex.It works for scenario 2,4,6 but doesn't work for scenario 1,3,5
Regex:
"(([a-zA-Z][a-zA-Z])| " +
"([a-zA-Z]*\\s\\'[a-zA-Z]*)|" +
"([a-zA-Z]*\\'\\s[a-zA-Z]*)|"+
"[a-zA-Z]*|" +
"[a-zA-Z]\\s|" +
"[a-zA-Z]\\'|" +
"\\s[a-zA-Z]|" +
"\\'[a-zA-Z]|"+
"\\s[a-zA-Z]*|"+
"\\'[a-zA-Z]*|" +
"[a-zA-Z]*\\s|"+
"[a-zA-Z]*\\')"
Code
Note: The link includes \r\n in the regex since the input is multiline
See regex in use here
^(?!(?:[^']*'){2})(?!(?:[^ ]* ){2})[a-z ']{2,}$
Results
Input
'abc
' abc
abc '
abc'
a 'bc
a' bc
abc
'
ab
a
a'' bc
a bc
Output
Below are only strings that match requirements.
Note: The second to last string sample is ' (apostrophe and space), which, according to the OP's requirements, should match.
'abc
' abc
abc '
abc'
a 'bc
a' bc
abc
'
ab
Explanation
^ Assert position at the start of the line
(?!(?:[^']*'){2}) Negative lookahead ensuring what follows doesn't include 2 apostraphes '
(?!(?:[^ ]* ){2}) Negative lookahead ensuring what follows doesn't include 2 spaces
[a-z ']{2,} Match two or more of the characters in the set
$ Assert position at the end of the line
Based on the provided explanation there is something smaller that you can do to fit the provided email.
^( |'|[a-zA-Z]){2,}
First value ^ will detect if it is a starting element and not a
substring.
Then we enclose in the parentheses all the possible
values, so, you can have a space, you can have an apostrophe or you
can have an a alphabetic string.
Finally, we consider than the
elements needs to have at least 2 characters, that's done with {2,}
If you want to limit the max length, you just need to add the number
in the right side. eg {2,10}.

How to get index of Escape Character in a String?

How to get index of Escape Character in a String?
String test="1234\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Result i get:
-1
Result i need: 4
Your original String doesn't contain \. Which means you are searching something which does not exist. Inorder to add \ to your string. You have to escape while adding
String test="1234\\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Should work.
In your case look at the last line in the table.
I don't think you can get that because when you use an escape character is for java to interpret the following character in a special way. In another words, the escape character and the next character you see in the string are really one entity from the point of view of program being executed.
When you search for "\\", you are searching for the literal character '\' not the escape character.
Here you can see the difference: java fiddle
While \5 is the character with code 0x5 (ENQ), 5 is the character 0x35. See the table.

Matching '_' and '-' in java regexes

I had this regex in java that matched either an alphanumeric character or the tilde (~)
^([a-z0-9])+|~$
Now I have to add also the characters - and _ I've tried a few combinations, neither of which work, for example:
^([a-zA-Z0-9_-])+|~$
^([a-zA-Z0-9]|-|_)+|~$
Sample input strings that must match:
woZOQNVddd
00000
ncnW0mL14-
dEowBO_Eu7
7MyG4XqFz-
A8ft-y6hDu
~
Any clues / suggestion?
- is a special character within square brackets. It indicates a range. If it's not at either end of the regex it needs to be escaped by putting a \ before it.
It's worth pointing out a shortcut: \w is equivalent to [0-9a-zA-Z_] so I think this is more readable:
^([\w-]+|~$
You need to escape the -, like \-, since it is a special character (the range operator). _ is ok.
So ^([a-z0-9_\-])+|~$.
Edit: your last input String will not match because the regular expression you are using matches a string of alphanumeric characters (plus - and _) OR a tilde (because of the pipe). But not both. If you want to allow an optional tilde on the end, change to:
^([a-z0-9_\-])+(~?)$
If you put the - first, it won't be interpreted as the range indicator.
^([-a-zA-Z0-9_])+|~$
This matches all of your examples except the last one using the following code:
String str = "A8ft-y6hDu ~";
System.out.println("Result: " + str.matches("^([-a-zA-Z0-9_])+|~$"));
That last example won't match because it doesn't fit your description. The regex will match any combination of alphanumerics, -, and _, OR a ~ character.

Categories

Resources