I have to create a program that reads in the number of character to be printed, and it will print random chars (a-z, A-Z, 0-9 and characters like !, &, $, etc). And the first character to be printed cannot be a number (0-9). So an example output would be like: Length of Variable? 20 a5fTnO$akP_a12BahsiO This is what I
Tag: char
check if char[] contains only one letter and one int
I have no idea how to check if char[] contains only one letter (a or b) on the first position and only one int (0-8) on the second position. for example a2, b2 I have some this, but I do not know, …
How do I use the character’s equals() method in Java?
As the String is an object, you need to use the string.equals(string) method to confirm that the two strings are equal. However, why do you use the == to check if two chars are equal, char1 == char2, rather than char1.equals(char2)? Answer Perhaps, this will help to understand the difference on == vs equals.
Cannot invoke charAt(int) on the array type String[]
I’m doing a function that counts digits in a string: The error is: Any ideas how to fix that? Answer You are trying to invoke the charAt() method on a String[]. String[] does not have such a method, but String does. What I believe you wanted to do is: This will get the char at position i in the String
Replace Unicode escapes with the corresponding character
I’m trying to convert code points, such as u00FC, to the character it represents. An example to explain what I mean: First Console line: Input: Hallu00F6 Second Console line: And Now: Hallö EDIT: Because sometimes it didn’t work with multiple Unicodes in The Trombone Willy’s answer, here is the Code fixed: Answer Joao’s answer is probably the simplest, but this
Java – Why can’t I use charAt() to see if a char equals another?
I want to see if a character in my string equals a certain other char value but I do not know what the char in the string is so I have used this: But I get the error: But “g” == “h” seems to work and I know you can use ‘==’ with char types. Is there another way to
How do I check if a string contains a list of characters?
How do I check if a list of characters are in a String, for example “ABCDEFGH” how do I check if any one of those is in a string. Answer use regular expression in java to check using str.matches(regex_here) regex in java for example:
Fastest way to iterate over all the chars in a String
In Java, what would the fastest way to iterate over all the chars in a String, this: Or this: EDIT : What I’d like to know is if the cost of repeatedly calling the charAt method during a long iteration ends up being either less than or greater than the cost of performing a single call to toCharArray at the
Most efficient way to convert a single char to a CharSequence
What’s the most efficient way to pass a single char to a method expecting a CharSequence? This is what I’ve got: According to the answers given here, this is a sensible way of doing it where the input is a character array. I was wondering if there was a sneaky shortcut I could apply in the single-char case. Answer
Count words, java
I want to count words. I use the methods hasNextChar and getChar. The sentence may contain all kind of chars. Here’s my code: It works so far but e.g. when I have a ” . ” at the end it gives me 8 instead of 7 words. Here are some examples of sentences: *„Schreiben Sie ein Praktikanten-Vermittlungs-Programm“ – words: 6