Please, can you give me a list of non printable characters in java programming? Thank you in advance.
Java uses the Unicode standard, so you should be asking about non-printable (non-printing?) characters in Unicode.
http://en.wikipedia.org/wiki/Unicode_control_characters
Java strings are unicode strings. Unicode doesn't have a concept of "non-printable" characters, exactly, but the ASCII non-printable range, along with several other characters, are considered Unicode control characters.
Are spaces printable? What about the private use area? Please modify the code to your definition of "printable" :)
import static java.lang.Character.*;
for (int i=0; i<MAX_CODE_POINT; i++) {
int t = getType(i);
boolean p = t == CONTROL || t == CONNECTOR_PUNCTUATION || t == CURRENCY_SYMBOL || t == DASH_PUNCTUATION || t == DECIMAL_DIGIT_NUMBER || t == ENCLOSING_MARK || t == END_PUNCTUATION || t == FINAL_QUOTE_PUNCTUATION || t == INITIAL_QUOTE_PUNCTUATION || t == LETTER_NUMBER || t == LOWERCASE_LETTER || t == MATH_SYMBOL || t == MODIFIER_LETTER || t == MODIFIER_SYMBOL || t == OTHER_LETTER || t == OTHER_NUMBER || t == OTHER_PUNCTUATION || t == OTHER_SYMBOL || t == START_PUNCTUATION || t == TITLECASE_LETTER || t == UPPERCASE_LETTER;
if (!p) {
System.out.println("Non printable codepoint " + i);
}
}
Related
So, I'm a complete novice, and I've been having trouble with this for a while. I have to edit this file so that all the vowels and punctuation are gone, and so that the first and last sentence is capitalized, along with the first letter of every word. I've got the first half done, but the second part is giving me trouble.
Here's what I've got so far:
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
public class SMcatcherInTheIO
{
public static void main (String [] args) throws Exception
{
File catcher= new File ("C:\\Users\\suvra\\Dropbox\\APCompSci 17-18\\AAA SM EXTRA CREDIT\\RyeCh1.txt");
BufferedReader br=new BufferedReader (new FileReader(catcher));
String rye;
while((rye=br.readLine()) !=null)
{
for (int i = 0; i < rye.length(); i++)
{
char c = rye.charAt(i);
if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u')
|| (c == ',') || (c == '.') || (c == '?') || ( c == '!'))
{
String front = rye.substring(0, i);
String back = rye.substring(i + 1);
rye = front + "" + back;
}
}
System.out.println(rye);
}
br.close();
}}
Create two String objects :Last and First sentence. Than Loop trought you code and stop at the first point and add value to firstSentence. Do similar for the last sentence just stop at the point-1. After that:
String lastSentence=somethingalreadydoneabove;
String upperCase=lastSentence.toUpperCase();
Hope this would help you.
DK
When the user enters their ID I want it to be in a specific format, they are mostly explained within the comments. I was wondering if their was an easier more efficient way of doing this. Also whether or not there is a way to change the entered letters to capital the way I've done the code, or any other method.
private boolean setCustomerID(String id) {
//Validates the customerID contains 3 letters a hypthen then 4 numbers
if ((id.charAt(0) < 'A' || id.charAt(0) > 'Z')
|| (id.charAt(1) < 'A' || id.charAt(1) > 'Z')
|| (id.charAt(2) < 'A' || id.charAt(2) > 'Z')
|| (id.charAt(3) != '-')
|| !isDigit(id.charAt(4))
|| !isDigit(id.charAt(5))
|| !isDigit(id.charAt(6))
|| !isDigit(id.charAt(7))) {
return false;
//Checks the user enters P, B or C for first letter
} else if ((id.charAt(0) == 'P' || id.charAt(0) == 'B' || id.charAt(0) == 'E')
//Checks the second and third letter are in the correct region
&& ((id.charAt(1) == 'S' && id.charAt(2) == 'C')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'C')
|| (id.charAt(1) == 'W' && id.charAt(2) == 'A')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'I')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'E')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'W')
|| (id.charAt(1) == 'M' && id.charAt(2) == 'I')
|| (id.charAt(1) == 'E' && id.charAt(2) == 'A')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'E')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'W'))){
// SC (Scotland), WA (Wales), NI (Northern Ireland), NE (North-East), NW (North-West),
//MI (Midlands), EA (East Anglia), SE (South-East), SW (South-West).
return true;
}
return false;
}
Use regex.
private boolean matchCustomerID(String id) {
return id.matches("^[PBE](?:SC|WA|NI|NE|NW|MI|EA|SE|SW)-\\d{4}\\b");
}
Regular expressions are one way of solving the problem. You can compose the pattern in a way that makes maintenance easier. Building on rcorreia's pattern, you can do something like:
private boolean setCustomerID(String id) {
char[] validFirstLetters = { 'P', 'B', 'E' };
String[] validRegions = { "SC", "WA", "NI", "NE", "NW", "MI", "EA", "SE", "SW" };
String pattern =
String.format("^[%s](?:%s)-\\d{4}$", new String(validFirstLetters),
String.join("|", validRegions));
return id.matches(pattern);
}
Note that this uses String.join() from Java 8. If you don't use Java 8 yet, consider using StringUtils from Apache Commons Lang.
Regexp is a great feature, but not easy to write and understand..
In this case, I would follow your way, but I would define some testing method. In this manner the code will be readable and easy to write Unit tests for it.
If you need some change later, you will understand the code.
Example:
testForLength();
testForLetters();
testForFirstTwoLetters();
I know there must be a simpler way to check, but this is what I'm doing right now.
if (g.charAt(0) == 'a' || g.charAt(0) =='b' || g.charAt(0) =='c' ||
g.charAt(0) == 'd' || g.charAt(0) =='e' || g.charAt(0) =='f' ||
g.charAt(0) == 'g' || g.charAt(0) =='h')
Relying on character ordering and that a..h is a consecutive range:
char firstChar = g.charAt(0);
if (firstChar >= 'a' && firstChar <= 'h') {
// ..
}
Use a regular expression for this one. Cut the first character of your String as a substring, and match on it.
if(g.substring(0, 1).matches("[a-h]") {
// logic
}
A variation on hemanth's answer:
if("abcdefgh".contains(g.substring(0,1))) do_something();
or
if("abcdefgh".indexOf(g.charAt(0)) >= 0) do_something();
Another way of doing it :
if(Array.asList("abcdefgh".toCharArray()).contains(g.charAt(0)))
{
//Logic
}
Currently I am checking a string for the following:
if(parseCommand.contains("vlan1") || parseCommand.contains("Fa0/1i")
|| parseCommand.contains("Fa0/1o") || parseCommand.contains("Fa1/0")
|| parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
|| parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
|| parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")
|| parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8")
|| parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11")
|| parseCommand.contains("Gi0")) {
//do things here
}
However it may contain vlan1 up to vlan4094 and i have to check for these. What is the simplest way to do this?
I have tried this just to match a vlan 1-9 folowed by 0-3 digits but it doesn't find anything:
if(parseCommand.matches(".*vlan[1-9](\\d){0,3}") || parseCommand.contains("Fa0/1i")
|| parseCommand.contains("Fa0/1o")|| parseCommand.contains("Fa1/0")
|| parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
|| parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
|| parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")
|| parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8")
|| parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11")
|| parseCommand.contains("Gi0")) {
Even if I try this nothing is found, why?
if(parseCommand.matches(".*vlan.*")
Use .matches("(?s).*vlan.*") or so for new line characters being catched by .; See DOTALL.
I created a method to validate a JTextField. When I am typing any alphabet character it should automatically convert into uppercase character, but I am not getting an uppercase.
How to solve this problem, when using evt.consume()?
Code snippet:
public void PRJ_TEXT_VALIDATION(JTextField PTxt, int PTxtLen, String POptnStr, KeyEvent Pevt){
String TmpStr=PTxt.getText();
char TmpChar=Pevt.getKeyChar();
//TmpChar=Character.toUpperCase(TmpChar);
if ((TmpStr.trim().length() + 1) <= PTxtLen){
if (POptnStr == "INTEGER") {
if (!((TmpChar>='0') && (TmpChar<='9'))){
Pevt.consume();
}
} else if (POptnStr == "NUMERIC"){
if(!((TmpChar>= '0' && TmpChar <= '9') || (TmpChar == '.'))){
Pevt.consume();
}
} else if (POptnStr == "ALPHABET"){
if(!(TmpChar>= 'a' && TmpChar <= 'z' || TmpChar >= 'A' && TmpChar <='Z')){
Pevt.consume();
}
} else if (POptnStr == "PHONE"){
if (!((TmpChar>= '0' && TmpChar <= '9') || (TmpChar == '-')||
(TmpChar == '+')||(TmpChar == '(')||(TmpChar == ')'))){
Pevt.consume();
}
}
}else{
Pevt.consume();
}
}
Use document filter to achieve this.
You can find usage in DocumentFilter that maps lowercase letters to uppercase.