It is a really simple question but I need an another eye to look at my code:
String strtr = "iNo:";
char[] queryNo = strtr.toCharArray();
System.out.println(queryNo + " =this is no");
and the output is:
[C#177b4d3 =this is no
What are these characters, do you have any idea?
That's how toString() is implemented for arrays.
The [C denotes that is a char array, 177b4d3 is its hashcode.
You may want to look at
System.out.println(Arrays.toString(queryNo) + " =this is no");
if you want to see your original String again, you need this:
System.out.println((new String(queryNo)) + " =this is no");
Arrays do not override toString(), it is inherited from Object.toString as
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
you are printing the object
queryno, as queryno is a character array of on dimension and java is an object oriented language which holds every thing in the form of classes it gives the class name [C to your array where [ denotes total dimension and C denotes character type of array, Rest is the hashcode of the object.
You are trying to print the array and that is the reason you get gibberish. Try using Arrays.toString(queryNo) and you will see what you expected.
Related
I have a char array that wrote in a file without any converting. the printed value to my file is [C#252ccf04 now I'm reading the file and this thing is being read as a string. now the retrieved data is [C#252ccf04 but this time it's a string. my problem is I want to assign it to another char array so I can read it using the Arrays.toString() method to reach the original value which is 123456 how can I accomplish that?
char[] pass = (data.substring(data.lastIndexOf(',') + 1).replaceAll(" ", "")).toCharArray();
System.out.println(Arrays.toString(pass));
// now the is: [[, C, #, 2, 5, 2, c, c, f, 0, 4]
and also I want to know what is this value what should I call it, is this a hashcode?
The value is the hashcode(as hex) plus the name of the class. According to the default toString() implementation:
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
You can't convert it back to the original character array. The hash code identifies the object while it is alive in memory. Once the program ends and the object dies, you can't get it back from the hashcode.
You should have saved the actual data as a String, or encrypted it yourself.
I have a question in this case how to convert char[] to string?(Must explicitly convert the char[] to a String) sorry being a novice programmer i am asking this.
String str3 = "Dad saw I was Playing";
System.out.println(str3.toCharArray() + "\n" + str3.toLowerCase() );
System.out.println(str3.toCharArray());
Output:
[C#1034bb5 //this is what I am getting for first str3.tochararray(),how to resolve this?
dad saw i was playing
Dad saw I was Playing
You can use Arrays.toString(char[]) (or create your own method if you prefer a different formatting). Something like,
String str3 = "Dad saw I was Playing";
System.out.println(Arrays.toString(str3.toCharArray()));
System.out.println(str3.toCharArray() + "\n" + str3.toLowerCase() );
This first calls toString over CharArray which outputs [C#1034bb5 and then append it to the rest of the string and print.
System.out.println(str3.toCharArray()); -- This calls the overridden method in PrintStream class which can interpret char array and print it.
You can explicitly convert your char array to string and print.
For the code
int []arr = new int[4];
System.out.println(arr);
The output looks something like
[I#54640b25
What exactly is the compiler printing out? The memory address of arr? Unlike C, Java does not seem to equate the array name (in isolation) with the first position of the array.
In Java, each object has toString() method, and arrays are objects. The default is displaying the class name representation, then adding "#" and then the hashcode:
The toString method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `#', and the unsigned hexadecimal representation of the hash
code of the object
Try to print the following line and you should get the same output:
int[] arr = new int[5];
System.out.println(arr.getClass().getName() + "#" + Integer.toHexString(arr.hashCode()));
Use the following in order to print the value of array:
Arrays.toString(arr);
Using System.out.println(arr) directory will print use the default toString method which returns:
object.getClass().getName() + "#" + Integer.toHexString(object.hashCode())
In a JFrame class I have a JPasswordField object, something like this:
pswdTextField = new JPasswordField(20);
externalPanel.add(pswdTextField, "w 90%, wrap");
I try to access to its inserted content (the password inserted by a user) by the following lines of code:
char[] pswd = pswdTextField.getPassword();
System.out.println("Password: " + pswd.toString());
The problem is that when I go to print this content I obtain the following output: Password: [C#d5c0f9 and not the inserted password
Why? What is it means? How can I obtain the inserted password?
Tnx
Andrea
Why? What is it means?
If you go through docs than you got this reasons.
For stronger security, it is recommended that the returned character
array be cleared after use by setting each character to zero.
How can I obtain the inserted password?
You can get password by,
char[] pswd = pswdTextField.getPassword();
String password=new String(pswd);
Or, you can directly print on System.out.print
System.out.print(pswd); // It override ...print(char[]) method
// without concat with another String.
Edit
Please note that, If you concat char[] with String than it will inherit Object.toString() method.
System.out.print("Password: " +pswd);// It will print like Password: [C#d5c0f9
pswdTextField.getText() is what you were looking for. toString() will not return the text inside a JPasswordField().
The toString method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `#', and the unsigned hexadecimal representation of the hash
code of the object. In other words, this method returns a string equal
to the value of: getClass().getName() + '#' +
Integer.toHexString(hashCode())
so use
System.out.println("Password: " +pswdTextField.getText());
getText() is depreciated for JPasswordField
in Java source, its written above getText() definition
" For security reasons, this method is deprecated. "
so getPassword() is preferred.
The problem you are facing is that you are printing an array using System.out.println()
either create a string using that character array
or use for loop to access each element one by one
for(int i=0;i < charArray.length;i++)
System.out.print (charArray[i]);
So I was just wondering if there was a way to print out multiple char variables in one line that does not add the Unicode together that a traditional print statement does.
For example:
char a ='A';
char b ='B';
char c ='C';
System.out.println(a+b+c); <--- This spits out an integer of the sum of the characters
System.out.println(a+""+b+""+c);
or:
System.out.printf("%c%c%c\n", a, b, c);
You can use one of the String constructors, to build a string from an array of chars.
System.out.println(new String(new char[]{a,b,c}));
The println() method you invoked is one that accepts an int argument.
With variable of type char and a method that accepts int, the chars are widened to ints. They are added up before being returned as an int result.
You need to use the overloaded println() method that accepts a String. To achieve that you need to use String concatenation. Use the + operator with a String and any other type, char in this case.
System.out.println(a + " " + b + " " + c); // or whatever format
This will serve : System.out.println(String.valueOf(a) + String.valueOf(b) + String.valueOf(c));.
System.out.println(new StringBuilder(a).append(b).append(c).toString());
System.out.print(a);System.out.print(b);System.out.print(c) //without space