Sign in to follow this  

[java] Adding a char to a String?

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a program to see if a word is a palindrome or not for AP Computer Science. Now, here's how the program will work generally.
import java.util.Scanner;

public class Palindrome
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter a potential palindrome: ");
		String palin = scan.nextLine();
		String result = "";
		
		
		
		
		for (int count = palin.length()-1; count >= 0; count--)
		{
			//Need help here
			//I want to append to the result String whichever char
			//is at palin.charAt(count)
			
		}
	}
}

So that's what it'll be doing. However, how do I append a char data type to a String object? I want to then just compare the strings to see if they are the same or not. Any help please? Thanks.

Share this post


Link to post
Share on other sites
Aha, thanks a bunch. I successfully got it working.
Here's how it goes now:


import java.util.Scanner;

public class Palindrome
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a potential palindrome: ");
String palin = scan.nextLine();
String result = "";




for (int count = palin.length()-1; count >= 0; count--)
{
char current[] = {palin.charAt(count)};

String toConcat = new String(current);

result = result.concat(toConcat);

}

palin = palin.toLowerCase();
result = result.toLowerCase();
System.out.println("\nYour word is: " + palin);
System.out.println("Backwards, this is: " + result);

if (palin.equals(result))
{
System.out.println();
System.out.println("That word is a palindrome!");

}
else
{
System.out.println();
System.out.println("That word is not a palindrome.");
}
}
}






Thanks for the help!

Share this post


Link to post
Share on other sites
Quote:
Original post by Thygrrr
Strings in Java are immutable; code such as String1 + String2 will compile into byte code that uses StringBuffer to do the concatenation :)


That is not guaranteed, just compare the performance and you will note that the second loop takes much longer:


StringBuilder sb = new StringBuilder();
for(int i=0; i<15000; i++)
sb.append(i);

System.out.println(sb);


String s = "";
for(int i=0; i<15000; i++)
s += i;

System.out.println(s);





Not using a StringBuilder/StringBuffer creates a new Object (String) every concateation, otherwise only when the char array buffer it is newly allocated.

A genral rule is: For few concateations or more with very short arguments the '+' operator is better, otherwise a StringBuilder/StringBuffer should be preferred. (Beside the fact that the latter one can handle char manipulation).


(Side Note: StringBuilder is a non-synchronized version of StringBuffer and potentially more performant, just if anyone is wondering)

Share this post


Link to post
Share on other sites

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this