Why is it my toString has error? - java

Why does my toString method have an error?
String score1 = Arrays.toString(newGame.top3Score[0]);
I am trying to take out the array value then convert them into strings.

Arrays.toString is expecting an array, yet you are only passing one element, try using:
Arrays.toString(newgame.top3Score)

Related

String.valueOf(someVar) vs ("" + someVar) [duplicate]

This question already has answers here:
String valueOf vs concatenation with empty string
(10 answers)
Closed 5 years ago.
I want to know the difference in two approaches. There are some old codes on which I'm working now, where they are setting primitive values to a String value by concatenating with an empty String "".
obj.setSomeString("" + primitiveVariable);
But in this link Size of empty Java String it says that If you're creating a separate empty string for each instance, then obviously that will take more memory.
So I thought of using valueOf method in String class. I checked the documentation String.valueOf() it says If the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
So which one is the better way
obj.setSomeString("" + primitiveVariable);
obj.setSomeString(String.valueOf(primitiveVariable));
The above described process of is done within a List iteration which is having a size of more than 600, and is expected to increase in future.
When you do "" that is not going to create an Object. It is going to create a String literal. There is a differenc(How can a string be initialized using " "?) actually.
Coming to your actual question,
From String concatenation docs
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
So unnecissarly you are creating StringBuilder object and then that is giving another String object.
However valueOf directly give you a String object. Just go for it.
Besides the performance, just think generally. Why you concatenating with empty string, when actually you want to convert the int to String :)
Q. So which one is the better way
A. obj.setSomeString(String.valueOf(primitiveVariable)) is usually the better way. It's neater and more domestic. This prints the value of primitiveVariable as a String, whereas the other prints it as an int value. The second way is more of a "hack," and less organized.
The other way to do it is to use Integer.toString(primitiveVariable), which is basically the same as String.valueOf.
Also look at this post and this one too

Alternative to printing a list in a text box?

Hi i'm using a GUI and at the moment printing a list to console. I understand you can print strings with text boxes using textBox.setText("") but instead of printing my list to console I want to use a text box or any other alternative.
At the moment ive got this as my printlist method:
private void printList(){
System.out.println(myList);
}
After attempting to do:
System.out.println(resultsBox.setText(myList));
I realize, this will not work as it only works with strings not lists. So yeah, what else could I use?
Thanks in advance
Do this to convert your list to a String,
if(myList!=null) {
resultsBox.setText(myList.toString());
}
and if there's a valid getText(), you can test the value that you set by printing it,
System.out.println(resultsBox.getText());
You just need to call
resultsBox.setText(myList)
separately.
Notice that this is a method call by itself that sets the values in the box. It does not return a value so you cannot pass it as an argument to println().

Difference between printing char and int arrays in Java [duplicate]

This question already has answers here:
If a char array is an Object in Java, why does printing it not display its hash code?
(6 answers)
Closed 5 years ago.
When I run the following code I get the address of the array:
int arr[] = {2,5,3};
System.out.println(arr); // [I#3fe993
But when I declare a character array and print it the same way it gives me the actual content of the array. Why?
char ch[] = {'a','b','c'};
System.out.println(ch); // abc
Class PrintStream (which is what System.out is) has a dedicated method overload println(char[]) which prints the characters of a char array.
It has no special overloads for other arrays, so when you pass an int[] the called method is println(Object). That method converts the passed object to a string by calling its toString() method.
The toString() method for all arrays is simply the default one inherited from class Object, which displays their class name and default hashcode, which is why it's not so informative. You can use Arrays.toString(int[]) to get a string representation of your int array's contents.
P.S. Contrary to what the doc says, the default hashcode of an object is not typically the object's address, but a randomly generated number.
When you say
System.out.println(ch);
It results in a call to print(char[] s) then println()
The JavaDoc for println says:
Prints a character and then terminate the line. This method behaves as though it invokes print(char) and then println().
A integer variable is not char, so the print(int[] s) get the address of array.

Inconsistent result for String.getBytes()

System.out.println("hello world".getBytes("UTF-8"));
occasionally returns a different value, why is that??
Sorry, I'm still a noob at Java.
This code prints an array (byte[]), but there is no standard array printing in Java. So instead of printing the content of the array, the code displays some cryptic memory reference to the array. Eg "[B#6bbc4459". This information is not very useful and is likely to change between programm executions.
If you want to display the content of the array, you must iterate through it.
You're printing the result of calling toString() on a byte array. That doesn't show you the contents, as arrays don't override toString() - it's just showing you something like [B#ABCDEF01 where the [B shows that it's a byte array, and the value after the # is a hash code.
If you want to show the byte array contents as numbers, you want something like Arrays.toString:
byte[] data = "hello world".getBytes("UTF-8");
System.out.println(Arrays.toString(data));

Confusion in print method in Java

Whenever I try to print the char arrays to the console, I'm getting the result in integer format, but whenever I try to print integer arrays to the console, I'm getting the result in hashcode format. Could anyone please tell me why?
char[] arr={'4','5','6'};
System.out.println(arr); //456
int[] arr={4,5,6};
System.out.println(arr) //[I#3e25a5]
java.io.PrintStream (the class of System.out) has a special print-method for char[], but not for int[]. So for the char[], this special method is used, while int[] is printed via the generic version, which prints the hashcode (or, to be more precise, the result of String.valueOf() called with the object as parameter).
Simply because there's no method for which handles int[] specially. It would be printed by String#valueOf() instead which implicitly calls Object#toString(). If the Object#toString() isn't overridden in the given object type, then the following will get printed (as per the aforelinked API).
getClass().getName() + '#' + Integer.toHexString(hashCode())
The int[] class has a name of [I.
To achieve what you want, you need Arrays#toString() instead:
int[] arr = {4, 5, 6};
System.out.println(Arrays.toString(arr)); // [4, 5, 6]
In the first case the character array is just used like a string (which is in fact also just an array of characters).
In the second it has no overload for the type of integer array and just prints out the object reference.
I think that the first one is treated as a CharSequence... like a String.

Categories

Resources