I have the following class:
public class Go {
public static void main(String args[]) {
System.out.println("G" + "o");
System.out.println('G' + 'o');
}
}
And this is compile result;
Go
182
Why my output contain a number?
In the second case it adds the unicode codes of the two characters (G - 71 and o - 111) and prints the sum. This is because char is considered as a numeric type, so the + operator is the usual summation in this case.
+ operator with character constant 'G' + 'o' prints addition of charCode and string concatenation operator with "G" + "o" will prints Go.
The plus in Java adds two numbers, unless one of the summands is a String, in which case it does string concatenation.
In your second case, you don't have Strings (you have char, and their Unicode code points will be added).
System.out.println("G" + "o");
System.out.println('G' + 'o');
First one + is acted as a concat operater and concat the two strings. But in 2nd case it acts as an addition operator and adds the ASCII (or you cane say UNICODE) values of those two characters.
This previous SO question should shed some light on the subject, in your case you basically end up adding their ASCII values (71 for G) + (111 for o) = 182, you can check the values here).
You will have to use the String.valueOf(char c) to convert that character back to a string.
The "+" operator is defined for both int and String:
int + int = int
String + String = String
When adding char + char, the best match will be :
(char->int) + (char->int) = int
But ""+'a'+'b' will give you ab:
( (String) + (char->String) ) + (char->String) = String
+ is always use for sum(purpose of adding two numbers) if it's number except String and if it is String then use for concatenation purpose of two String.
and we know that char in java is always represent a numeric.
that's why in your case it actually computes the sum of two numbers as (71+111)=182 and not concatenation of characters as g+o=go
If you change one of them as String then it'll concatenate the two
such as System.out.println('G' + "o")
it will print Go as you expect.
Related
I'm presently trying to understand a particular algorithm at the CodingBat platform.
Here's the problem presented by CodingBat:
*Suppose the string "yak" is unlucky. Given a string, return a version where all the "yak" are removed, but the "a" can be any char. The "yak" strings will not overlap.
Example outputs:
stringYak("yakpak") → "pak"
stringYak("pakyak") → "pak"
stringYak("yak123ya") → "123ya"*
Here's the official code solution:
public String stringYak(String str) {
String result = "";
for (int i=0; i<str.length(); i++) {
// Look for i starting a "yak" -- advance i in that case
if (i+2<str.length() && str.charAt(i)=='y' && str.charAt(i+2)=='k') {
i = i + 2;
} else { // Otherwise do the normal append
result = result + str.charAt(i);
}
}
return result;
}
I can't make sense of this line of code below. Following the logic, result would only return the character at the index, not the remaining string.
result = result + str.charAt(i);
To me it would make better sense if the code was presented like this below, where the substring function would return the letter of the index and the remaining string afterwards:
result = result + str.substring(i);
What am I missing? Any feedback from anyone would be greatly helpful and thank you for your valuable time.
String concatenation
In order to be on the same page, let's recap how string concatenation works.
When at least one of the operands in the expression with plus sign + is an instance of String, plus sign will be interpreted a string concatenation operator. And the result of the execution of the expression will be a new string created by appending the right operand (or its string representation) to the left operand (or its string representation).
String str = "allow";
char ch = 'h';
Object obj = new Object();
System.out.println(ch + str); // prints "hallow"
System.out.println("test " + obj); // prints "test java.lang.Object#16b98e56"
Explanation of the code-logic
That said, I guess you will agree that this statement concatenates a character at position i in the str to the resulting string and assigns the result of concatenation to the same variable result:
result = result + str.charAt(i);
The condition in the code provided by coding bat ensures whether the index i+2 is valid and then checks characters at indices i and i+2. If they are equal to y and k respectively. If that is not the case, the character will be appended to the resulting string. Athowise it will be discarded and the indexed gets incremented by 2 in order to skip the whole group of characters that constitute "yak" (with a which can be an arbitrary symbol).
So the resulting string is being constructed in the loop character by characters.
Flavors of substring()
Method substring() is overload, there are two flavors of it.
A version that expects two argument: the starting index inclusive, the ending index, exclusivesubstring(int, int).
And you can use it to achieve the same result:
// an equivalent of result = result + str.charAt(i);
result = result + str.substring(i, i + 1);
Another version of this method, that expects one argument will not be useful here. Because the result returned by str.substring(i) will be not a string containing a single character, but a substring staring from the given index, i.e. encompassing all the characters until the end of the string as documentation of substring(int) states:
public String substring(int beginIndex)
Returns a string that is a substring of this string. The substring
begins with the character at the specified index and extends to the
end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
Side note:
This coding-problem was introduced in order to master the basic knowledge of loops and string-operations. But actually the simplest to solve this problem is by using method replaceAll() that expects a regular expression and a replacement-string:
return str.repalaceAll("y.k", "");
public class ArrayIntro
{
public static void main(String[] args)
{
int[] a;
a=new int[10];
Scanner sc =new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int e:a)
{
System.out.print(e+' ');
}
}
}
Input:1 2 3 4 5 6 7 8 9 10
Output:33343536373839404142
Its mean it added 32 to each number
Just try with below code.
As you are printing sout inside for loop, it's printing sum of number and Space( ' '). And space have ASCII value of 32, so you are able to see every element with added value of 32.
For ASCII Or Unicode you can refer this link, it will help you.
Simply you put something like this System.out.print(new Integer(' '));, it will print 32.
If you want to add space only then go with double quote.
System.out.print(e+" ");.
Single quote consider as character and charater added with Integer will summing up with their ASCII code.
public static void main(String[] args)
{
int[] a;
a=new int[10];
Scanner sc =new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int e:a)
{
System.out.print(e+" ");
}
}
To put it simply, ' ' is a character in ASCII and the value of it is 32. So, as far as I know, you might want it the just print directly so you can just replace ' ' with " ". Always remember that single quotes are for characters and double quotes are for strings.
For your answer you can just do this:System.out.print(e+" ");
Instead of this: System.out.print(e+' ');
In short, to fix your program, you need to use " " (with double quotes) instead of ' '.
The issue is that the + operator has two different meanings in Java.
If either of the operands is a String, then + means "concatenate these two operands together". If the other operand is not a String, then it will have to be converted somehow, and there are some rules around how to do this.
If neither operand is a String, then + means "add these two operands together" - that is, they are treated as numbers. They can be added as double values, float values, long values or int values, depending on the data types. But if they are short, byte or char, they will be treated as int. This is called binary numeric promotion, and is explained in detail in the Java Language Specification.
Now in Java, single quotes are used to delimit a char literal. So when you write e + ' ', you're adding an int to a char. This is addition - the char is treated as an int, and the operands are added as numbers. The number corresponding to is 32, since that's the numeric value in the character encoding that Java uses, which is something like UTF-16. Contrary to some other answers on this page, it's not ASCII.
Double quotes are used to delimit a String literal. So if you write e + " ", the operands are an int and a String. That means you're not adding any more, but concatenating. The int is converted to a String first, then concatenated to " ", which will give you the result you're expecting.
Hi im trying to make a letter guessing game with 3 random letters, when i try to get a value using the below code it always a returns as an int to the other program bellow im trying to receive it as a letter but can't figure it out.
import java.util.Random;
public class CodeLetter {
private char letterValue;
int count = 8;
Random rnd = new Random ();
public char codeLetter(){
letterValue = (char)(rnd.nextInt(5)+'A');
System.out.println(letterValue);
return letterValue;
}
}
The code that calls the above code using line
letter1 = codeLetter.codeLetter();
but once it is printed to screen it still holds an int value, not the char.
This program requires both classes as requirments
public class CodeBreaker {
private char letter1;
private char letter2;
private char letter3;
CodeLetter codeLetter = new CodeLetter();
public void CodeBreaker(){
//Welcome Screen
System.out.println("Welcome to CODEBREAKER ");
System.out.println("you have 6 tries to guess the secret 3 letter code.");
System.out.println("The letters Range from A to E");
System.out.println("Goodluck");
System.out.println("The code has no repeat letters");
//end
letter1 = codeLetter.codeLetter();
letter2 = codeLetter.codeLetter();
letter3 = codeLetter.codeLetter();
System.out.println(letter1 + letter2 + letter3);
}
/*public boolean done(){
}
/*private boolean isValid*char){
//- Is the given letter valid?
}*/
public void getGuess(/*int*/){
//- Get guess #
}
public void checkGuess(){
//- Verify the guess
}
public void display(){
//- Display the secret code
}
}
I have run your program. Bellow output I have found.
Welcome to CODEBREAKER
you have 6 tries to guess the secret 3 letter code.
The letters Range from A to E
Goodluck
The code has no repeat letters
B
B
C
199
If I understand you correctly You have problem with that 199. I assume, you want BBC instead of 199. It is because you use + sign with the char type that will implicitly converted into integer and do arithmetic operation. So you have problem with the following line System.out.println(letter1 + letter2 + letter3);. You can print separately those different char.
BTW, your codeletter() method return char as expected. Also for this kind of language behavior you can researh on Strongly vs Weakly type
The + operator is either a numeric additive operator or a string concatenation operator.
The Java Language Specification, §15.18. Additive Operators, say:
If the type of either operand of a + operator is String, then the operation is string concatenation.
Otherwise, the type of each of the operands of the + operator must be a type that is convertible (§5.1.8) to a primitive numeric type, [...]
Your code (letter1 + letter2 + letter3) is a char + char + char expression, and since none of them are String values, it's a numeric add expression.
You have many choices for fixing that:
Create a String directly from a char[]:
new String(new char[] { letter1, letter2, letter3 })
This is the most efficient way, both performance-wise and memory-wise.
I recommend doing it this way.
Convert the first letter to a String, so the + operator becomes a string concatenation operator:
String.valueOf(letter1) + letter2 + letter3
This is shorter to write, but is less efficient, since it first creates a temporary string, then have to perform a string concatenation to build the final string.
Another way to convert the first letter to a String:
Character.toString(letter1) + letter2 + letter3
Internally, Character.toString() calls String.valueOf() (Java 8), so it's really the same, and you can use whichever you like best.
Perform the string concatenation directly, so you don't need to create the initial temporary string:
new StringBuilder().append(letter1).append(letter2).append(letter3).toString()
Very verbose.
Start with an empty string:
"" + letter1 + letter2 + letter3
This is the most terse way to write it. A lot of people like this way because of the terseness, and I believe the minimal overhead of the extra string will even be eliminated in Java 9.
What you are looking for is Character.forDigit()
I am using a NoSQL database which doesn't allow equality conditions of attributes that are projected. Eg Unequality operations such as select a from table where a > 10 and is allowed select a from table where b < 10, but select a from table where a = 10 is not allowed. Of course I need to use an equality in my case, so I want to turn a equality operations into an inequality operation.
So I need to retrieve a record by email. If could I would go select email from table where email = 'myemail#email.com', but this is not allowed so I want to get the lexicographic value right before myemail#email.com and the value right after. So the query would look like this:
select email from table where email < [1 value above] and email > [1 value below]
This way the statement would still return myemail#email.com. I am having trouble though how to accomplish this.
Usually to compare strings I go "myemail#email.com".compare("myemail#email.ca") to see which one bigger and which one is smaller. This method compares the values somehow based on lexicographic, but how? My question is how to get the lexicographic value right below a string and the lexicographic value right after the string?
The string immediately after a string is easy. It's just
str + '\0'
This works because '\0' is the lowest possible char value.
The string immediately before str is more tricky. If the string ends in '\0' you can just remove it. If the string doesn't end in '\0' you have serious issues. As an example, let's consider the string "foo".
Each of the following strings is below "foo" and each one is bigger than the last.
"fon" + Character.MAX_VALUE;
"fon" + Character.MAX_VALUE + Character.MAX_VALUE;
"fon" + Character.MAX_VALUE + Character.MAX_VALUE + Character.MAX_VALUE;
...
The largest String value less than "foo" is "fon" followed by something like 2^31 - 4 copies of Character.MAX_VALUE (this may not be right. I'm not sure what the largest possible length of a char[] is). However, you will not be able to store such a string in memory.
You should therefore try to find an different solution to your problem.
Assuming your alphabet is a-z0-9, and case-insensitive, you can treat your string as a base-36 number and simply increment/decrement the values using simple arithmetic.
Java's Long.valueOf method allows you to take a String with a given radix, and convert it to it's (base 10) Long equivalent. Once you have a Long instance, you can simply add 1 to get the next value.
public static String nextString(String str) {
return Long.toString(Long.valueOf(norm(str), 36) + 1, 36);
}
To reverse the operation, you can use the Long.toString method, which takes a long instance and converts it to a String representation, with a specified radix. So you can represent your base-10 long as a base-36 number, which will include the letters a-z.
public static String prevString(String str) {
return Long.toString(Long.valueOf(norm(str), 36) - 1, 36);
}
You'll want to normalize your strings when using these methods, so this will filter our invalid characters, ensure that everything is lower-case, and prevent null pointer exceptions or number format exceptions.
private static String norm(String str) {
if (str == null) {
return "0";
}
return str.toLowerCase().replaceAll("[^a-z0-9]", "");
}
the original question is like this.
public class test {
public static void main(String[] args){
int i = '1' + '2' + '3' + "";
System.out.println(i);
}
}
and this gives me an error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to int
then I changed the code like this:
public class test {
public static void main(String[] args){
int i = '1' + '2' + '3';
System.out.println(i);
}
}
the out put is 150.
but when I write my code like this:
public class test {
public static void main(String[] args){
System.out.println('a'+'b'+'c'+"");
}
}
the output become 294.
I wonder why.
The first one does not compile, because you concatenate a String at the end which cause the value to be a String which can't be converted directly to int.
The output of the second one is 150, because ASCII value for character 1,2,3 are 49,50,51 which return 150 when doing the addition.
The output of the last one is 294, because you are doing an addition of char values in the ASCII table (97+98+99)
You can verify the values here for a,b and c (or any other character).
Edit : To explain why the last one output the correct value instead of throwing an error, you first sum all the values as explained before, then convert it to a String adding "" to the sum of the ASCII values of the chars. However, the println method expect a String which is why it does not throw any error.
The first one would work if you would do Integer.parseInt('1' + '2' + '3' + "");
When you do this
int i = '1' + '2' + '3';
the JVM sums the ASCII codes of the given numbers. The result is 150.
When you add the empty String, you are trying to sum an int/char with a String. This is not possible. You can implicitly convert char to int and vice versa because they are primitive types. You cannot do this with String objects because they are not primitives but references. That's why you get an error.
When you do the println the primitive values are firstly summed and the automatically boxed into reference type so the sum is boxed into a Character object. The empty String is converted to a Character and then is added to the first one. So the result is a Character object that has an ASCII code 294. Then the toString method of the Character is called because that's what the println(Object) method does. And the result is 294
I hope this will help you to understand what is happening :)
The first is impossible because you can't convert String to int this way.
The second works because chars are kind of numbers, so adding chars is adding the numbers they really are. Char '1' is the number 49 (see ASCII table), so the sum is 49+50+51 which is 150.
The third works this way because + is a left parenthesized operator, which means that 'a'+'b'+'c'+"" should be read as (('a'+'b')+'c')+"". 'a' has ASCII code 97, so you have 294+"". Then Java knows that is should convert the value to a String to be able to catenate the two strings. At the end you have the the string 294. Modify your last code to the following System.out.println('a'+'b'+('c'+"")); and you will see that the result will be 195c.
You must note that System.out.println is a method that is used to convert values (of different types) to their String representation. This is always possible as every int can be converted to a String representation of it, but not the converse; not every String is a representation of an int (so Java will not let you do it so simply).
First: [int i = '1' + '2' + '3' + "";]
If you concat an empty string value, you convert it to a String object, and then String objects can't convert to int.
Second: [int i = '1' + '2' + '3';]
The binary arithmetic operations on char promote to int. It's equal to:
[int i = 49 + 50 + 51] - total: 150.
Third: [System.out.println('a'+'b'+'c'+"");]
At this case you convert 'a' + 'b' + 'c' (that is 294) to String (+"") and then print the result like a String value and that works ok.