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.
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 1 year ago.
Improve this question
I have declared a variable of type long and I want to print the statements if the given variable lies between the range specified -
class Sample {
public static void main(String[] args) {
long n = 3454546L;
if (-2147483648L<=n && n>=2147483647L) {
System.out.println("* int");
System.out.println("* long");
}
}
}
This is the code.
Because your logic is is n >= -2147483648L true and n >= 2147483647L false so overall false?
Think you wanted
-2147483648L<= n && n<=2147483647L
Your second part being n>=2147483647L is not matching, thus you have false and will not go into the statements, i guess it should be n<=2147483647L?
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 want to build a loop where the console keeps asking me for a string until I write the word "out". This is what I have till now, but it doesn't loop and I can't figure out why. Also I really want to use while or do while if possible.
Scanner input = new Scanner(System.in);
String name = input.nextLine();
while (!name.equals("out")) {
System.out.println(name);
break;
}
You're not updating name inside the loop and remove the break otherwise you're jumping out of the loop in the first iteration (which is not what you want)
while (!name.equals("out")) {
System.out.println(name);
name = input.nextLine(); // I've added it here for you
}
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);
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 6 years ago.
Improve this question
I have this sample from my project and I need to know why the result is what it is.
public class Main
{
public static void main(String[] args)
{
//url: https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup, websiteList: http://classicpartyrentals.com/, URL Contains Returns bool: false
String url = "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup";
String contains = "http://classicpartyrentals.com/";
System.out.println("Returns bool: " + url.contains(contains));
}
}
Output:
Returns bool: false
Code is always doing what you ask it to do:
String url = "https://classicpartyrentals.com/products/24681-gothic-
but
String contains = "http://classicpartyrentals.com/";
https versus http!
So the real answer is: especially when you are a beginner, chances that your code uncovered "some Java bug" is relatively small (very close to zero in reality!)
There is a much higher chance that your assumptions are wrong. You either don't fully understand the methods you are calling, or there is a subtle flaw in your input data.
Finally: also work on your naming. contains isn't a very good name here; you better call it expectedUrl, or something alike!
In your Code Url "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup" contains https
but your compare string "http://classicpartyrentals.com/" contain http so its not match and return false
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