Skip to main content
gamedev.net
🔒 Locked

[java] Adding a char to a String?

Started by Lucky Ace Card Sep 26, 2006 at 10:40 PM 5 replies 1.1k views
Original Post
Lucky Ace Card
Lucky Ace Card
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.
Fruny
Fruny
This says that you can create a string object from a single character and concatenate two strings (producing a new object - strings are immutable).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Lucky Ace Card
Lucky Ace Card
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!
Shabadoo
Shabadoo
You could use StringBuffer and the append() method. Then just do a toString() on the buffer when it's time to compare

Cheers,
Brett
Thygrrr
Thygrrr
Strings in Java are immutable; code such as String1 + String2 will compile into byte code that uses StringBuffer to do the concatenation :)
Michael Nischt
Michael Nischt
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)
Thygrrr
Thygrrr
Yes, of course you're right. Sorry if I pointed that out wrong!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.