Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'd like to write a small program in Java 8 which prints out a list of characters in reverse lexicographic order. For example, "stack" should become "tskca". I entered this code:
public static void main (String[] args) {
String input = "abc";
char[] charArray = input.toCharArray();
List<Character> charList = new ArrayList<Character>();
for (char c : charArray) {
charList.add(c);
}
System.out.println(charList.toString());
charList.sort(Collections.reverseOrder());
String output = charList.toString();
System.out.println(input);
}
The output will be:
abc
I tried it with
Collections.sort(charList, Collections.reverseOrder());
instead the other sorting line, same result. I looked up similar questions, but didn't find the same issue. Did I mess something up?
Just a little typo... Change
System.out.println(input);
to
System.out.println(output);
I think it helps to print the correct String ;)
System.out.println(output);
If you want it to print cba instead of [c, b, a], than use for-each:
charList.stream().forEach(System.out::print);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been playing around with the printf method and I am confused with the output from this code:
public static void main(String[] args) {
String[] names = {"Sam, John, George"};
double[] balances = {1000.97,100.00,87.00};
for(int i=0; i<names.length; i++){
System.out.printf("Hello %s this is your balance %.2f\n",names[i],balances[i]);
}
}
The output: Hello Sam, John, George this is your balance 1000.97
I expected three print statements in the console for each person.
Thank for any help.
"Sam, John, George" is one string.
You're missing some quotes. It should be:
String[] names = {"Sam", "John", "George"};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to find the palindrome of a string.
public static void main(String[] args)
{
String s1 = "eye",s2="";
for(int i = s1.length()-1;i<=0;--i)
{
s2 =s2+String.valueOf(s1.charAt(i));
}
System.out.println(s1);
System.out.println(s2);
}
}
I expected the output
eye
eye
but, s2 isn't printing.
You have the wrong condition in the for loop. The correct condition should be i>=0.
Remember that as long as this condition is true, the for loop will run. Your original condition, i<=0 is false at the very beginning, when i is 2, so the for loop never starts.
A less important problem is that you should not concatenate strings in a for loop, and should use a StringBuilder instead. See this.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to get an average of an array and I found this code during my research.
int myArray[] = {1,2,3};
Arrays.stream(myArray).average();
System.out.println(Arrays.toString(myArray));
When I run it, the results come up as
[1, 2, 3]
I'm new at java and I feel like there's something obvious that I'm not seeing.
You are printing the original Array.
To print the calculated array add the next line:
System.out.println(Arrays.stream(myArray).average().getAsDouble());
Output:
2.0
You are just printing only the array, Instead, you can do this.
int myArray[] = {1,2,3};
System.out.println(Arrays.toString(Arrays.stream(myArray).average().getAsDouble()));
OR
int myArray[] = { 1, 2, 3 };
OptionalDouble ans = Arrays.stream(myArray).average();
System.out.println(ans.getAsDouble());
Here,OptionalDouble is a container object which may or may not contain
a double value. If a value is present, isPresent() will return true
and getAsDouble() will return the value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm writing a little application that receives instructions in a StringBuilder filled with "(" and ")", for every ( the application sums 1, for every ( it decreases 1. I know it's a bit weird but it's a puzzle and it had to be done like that.
I'm having problems reading the content from the stringbuilder, Eclipse is complaining that it cannot access the specified index with charAt, what am I doing wrong?
I already checked and StringBuilder instructions is valid and properly filled with ( and ), no nulls or other symbols.
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==")"){
//do something
}
else(){
//do other thing
}
}
To use a Char in Java you use ' not "
so it should be :
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==')'){ //<-- note the change of " to '
//do something
}
else{
//do other thing
}
}
}
Edit 1:
The above solves the complication problem however
as #cricket noted, it should be charAt(i) for correctness of the code
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I try this code:
byte[] data = new byte[66000];
int count = is.read(data);
String sRequest = new String(data); //will received byte array hello
String all = sRequest;
all = all.concat("world");
System.out.println(all);
It only print to my console: hello
concat funtion of java have bug? I also used + operator instead concat function but result same :(
How can I concat a String with new String from a byte array?
Instead of
String sRequest = new String(data); //will received byte array hello
use
String sRequest = new String(data, 0, count); //will received byte array hello
You will notice the difference when you additionally print the length of the result string:
System.err.println(all + "/" + all.length());
gives helloworld/66005 in the first case and helloworld/10 in the second case. The reason you only see "hello" might be an issue of your console - in Eclipse, I do see "helloworld" but when I copy&paste that into another editor only one of the words is taken. The 0 values from the initial array are part of the result (since they already had been added to the first string), but they are not printed out (since they are not printable characters).