Print character array to console in Java [duplicate] - java

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 5 years ago.
I'm having a problem when I try to parse a String to a char array.
Here is my code:
line = scan.nextLine();
System.out.println(line);
char line2[] = line.toCharArray();
System.out.println(line2.toString());
So, as you can see, it's a simple code.
The problem is: line contains the string "00000001010010110100100000100000", but when I use "line.toCharArray", my char array receives "[C#7e243eed".
I think it's receiving the line variable adress or something like that.
Could someone help me?
Thanks :D

String to char array conversion is fine, it's Sysout that prints hash of the array object instead of its content. If you want to print the array in user friendly way, use:
System.out.println(Arrays.toString(line2));

Related

How to use a java string method to return a selected amount of characters? [duplicate]

This question already has answers here:
Is there a Java equivalent to Python's Easy String Splicing?
(8 answers)
Closed 2 years ago.
I am trying to figure out how to return a selected amount of characters in my java string.
In Python, it is just like
randomVar = "hello"
print(randomVar[1:])
and the output would be "ello".
How do I do the same thing in java?
Thank you!
This can be accomplished with substring()
String a = "hello";
System.out.println(a.substring(1, a.length());

create 2-D character array in java [duplicate]

This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
Closed 3 years ago.
I want to create a character array like this one below:
character array
i am unable to find a method . Please help me out. Beginner in java
char[][] x = new char[][]{};
x[0][1] = 'c';
...

Java ArrayList<String> to single string without using loop [duplicate]

This question already has answers here:
A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
(15 answers)
Best way to convert an ArrayList to a string
(27 answers)
What's the simplest way to print a Java array?
(37 answers)
Java function for arrays like PHP's join()?
(24 answers)
Closed 3 years ago.
I have an ArrayList<String> that is added to periodically. What I want to do is to cast the entire ArrayList to a String without doing a loop. Can anybody tell me is it possible without using loop?
Edited:
So we found some solutions, Like
list.stream().collect(Collectors.joining());
Or
String result = String.join(",", list);
or some others as well. Now Just for getting knowledge I put a question which one is the most optimal way for compiler?
You could make a stream out of your list and collect it using joining collector :
list.stream().collect(Collectors.joining());
You can pass the separator to Collectors::joining method for example :
list.stream().collect(Collectors.joining("-"));
Or you can use String::join method :
String result = String.join(",", list);
where , is the separator.
I Believe you can do
String.join(", ", list);
Hope this helps :)
Given some ArrayList<String> s, all you need to use to get a String representation is s.toString().
The format is the following
[element1, element2, element3]

Why is the code misbehaving with tabs? [duplicate]

This question already has answers here:
Getting error on printing array
(4 answers)
Closed 6 years ago.
char[] array = {'h','e','l','l','o','h','i','e',' '};
System.out.println(array+"\t"+array.length);//FIX THIS
It prints :
[C#15db9742 9
But if I change the code to :
char[] array = {'h','e','l','l','o','h','i','e',' '};
System.out.println(array);//FIX THIS
It prints :
hellohie
Why can't I put a tab and a number in concatenation? Because the following code worked fine:
String array1 = new String(array);
System.out.println(array1+'\t'+array1.length());
PrintWriter
It has multiple println methods, when you try to System.out.println(array) it inokves the one with char array as the argument, that's why it prints nicely. When you try to invoke your different version, it doesn't know what you expect it to do with that array, that's why it doesn't print nicely. Tab has nothing to do with that.
You can, for example, do System.out.println(Arrays.toString(array)+"\t"+whatever), or convert it to String before like you did, or a lot of other solutions.

Java Array returning weird output [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm creating a method for a homework problem that returns the highest value in an array. I'm using a for
loop to input the numbers into the array. Here is what the input code looks like.
int[] array = new int [n];
for(i=0;i<array.length;i++){
array[i]=sc.nextInt();
System.out.println(array);}
When i run the program to try and see if there's anything wrong as a I go along I notice the array not returning the value I input into it. For example
putting the number 12 it returns [I#6c80d028. I'm pretty new to java so I don't really know what's going on with the input. Is there any thing that can be done?
Please use:
System.out.println(Arrays.toString(array));
Please do this
System.out.println(array[i]);
or
System.out.println(Arrays.toString(array));
when you
System.out.println(array);
It prints the address and not the value hence you see a junk characters / not expected value.

Categories

Resources