Topic
1 reply Latest Post - ‏2007-10-02T18:02:49Z by SystemAdmin
SystemAdmin
SystemAdmin
1240 Posts
ACCEPTED ANSWER

Pinned topic Noob Question

‏2007-10-02T10:17:53Z |
Ok, I've been workin with Java for just about a week so bear with me. It's my first real programming language, although I consider myself very advanced in JavaScript and mIRCscript, so I am familiar with the concepts involved in programming.

What I'm trying to write is a program that will let you input a string, and then tell if that string is a palindrome. Here's what I've got so far (it's incomplete):
codeimport java.util.Scanner;

public class PalindromeTester
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);

String input;
int offset = 0;
char build[];

System.out.print("Enter a string: ");
input = scan.nextLine();

//Convert input into a character array

for (int length = input.length(); length >= 0; length--)
{
build = input.getChars(length, length, build, offset);
offset++;
}

System.out.print(build); //for debugging
}
}[/code]

Basically all this is attempting to do is take the input from the user into [b]input[/b] and then use a for loop to convert it into a character array that contains the contents of input in reverse order called [b]build[/b].

The compile-time error I get is:
codePalindromeTester.java:20: incompatible types
found : void
required: char[]
build = input.getChars(length, length, build, offset);
^
1 error[/code]
And I really don't understand this error..getChars, I thought, accepted a string and outputs into a char array, just like I'm trying to do. What's this about void?
Updated on 2007-10-02T18:02:49Z at 2007-10-02T18:02:49Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1240 Posts
    ACCEPTED ANSWER

    Re: Noob Question

    ‏2007-10-02T18:02:49Z  in response to SystemAdmin
    One of the very first thing you should discover in your Java journey is the JDK API documentation (for 1.5: http://java.sun.com/j2se/1.5.0/docs/api).

    You would then discover the Java doc for getChars:

    public void getChars(int srcBegin,
    int srcEnd,
    char[] dst,
    int dstBegin)

    You would then notice the return type is void, hence can not be assigned to a char[], as you try to do.