Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

JAVA Confused


  • Please log in to reply

#1
chancit2003

chancit2003

    Member

  • Member
  • PipPip
  • 15 posts
I have to write a program that I input a string and it tells me whether or not it is a Palindrome...here is what I have


public class Palindrome {
public static void main (String[] args) {

String inStr;
StringBuffer revStr;

// Get the string that is to be checked to
// see if it is a palindrome.
System.out.println("Palindrome Checker...");
System.out.print("Enter a string to check: ");
inStr = Console.in.readLine();

// Create a new StringBuffer object containing the
// same characters as inStr and make revStr
// refer to the new object.
revStr = new StringBuffer(inStr);

// Use the reverse() mutator method of the StringBuffer
// class on the StringBuffer object refered to by revStr.
revStr.reverse();

// Check to see if the inStr and revStr are the same.
// This requires using the .equals() or .equalsIgnoreCase()
// accessor method of the String class. These methods accepts
// a String so the StringBuffer refered to by revSrt must be
// converted to a String...
if (inStr.equalsIgnoreCase(revStr.toString())) {
System.out.println(inStr + " is a palindrome.");
}
else {
System.out.println(inStr + " is not a palindrome.");
}
}
}

I get an error on the following line: inStr = Console.in.readLine();
when I try to compile it..I get the following error:
Palindrome.java:19: cannot resolve symbol
symbol : class in
location: package Console
inStr = Console.in.readLine();
with a little arrow pointing to the "." between the Console and the in.

Yes I am a beginner so the simpler the better,

Thanks
Kim
  • 0

Advertisements


#2
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hello,

I added a few lines to your code and you will be able to compile. It shows you one basic way to do I/O with the basic java libraries. I will cut and paste it below and will attach the java file in a zip archive. The main additions were the stdin and console variable to allow us to grab input from the console. With readLine(), we have to catch any errors, hence the "try" and "catch" statements. Only other change was to initialize the inStr variable since to Java, it may not get initialized since the assignment statement to inStr is in a "try" statement and could potentially not occur. And the program works great! :^)

import java.io.*;

public class Palindrome {
public static void main (String[] args) {

String inStr = new String();
StringBuffer revStr;

InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);

// Get the string that is to be checked to
// see if it is a palindrome.
System.out.println("Palindrome Checker...");
try
{
System.out.print("Enter a string to check: ");
inStr = console.readLine();
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}

// Create a new StringBuffer object containing the
// same characters as inStr and make revStr
// refer to the new object.
revStr = new StringBuffer(inStr);

// Use the reverse() mutator method of the StringBuffer
// class on the StringBuffer object refered to by revStr.
revStr.reverse();

// Check to see if the inStr and revStr are the same.
// This requires using the .equals() or .equalsIgnoreCase()
// accessor method of the String class. These methods accepts
// a String so the StringBuffer refered to by revSrt must be
// converted to a String...
if (inStr.equalsIgnoreCase(revStr.toString())) {
System.out.println(inStr + " is a palindrome.");
}
else {
System.out.println(inStr + " is not a palindrome.");
}
}
}

Attached Files


  • 0

#3
chancit2003

chancit2003

    Member

  • Topic Starter
  • Member
  • PipPip
  • 15 posts

Hello,

I added a few lines to your code and you will be able to compile. It shows you one basic way to do I/O with the basic java libraries. I will cut and paste it below and will attach the java file in a zip archive. The main additions were the stdin and console variable to allow us to grab input from the console. With readLine(), we have to catch any errors, hence the "try" and "catch" statements. Only other change was to initialize the inStr variable since to Java, it may not get initialized since the assignment statement to inStr is in a "try" statement and could potentially not occur. And the program works great! :^)

import java.io.*;

public class Palindrome {
public static void main (String[] args) {

String inStr = new String();
StringBuffer revStr;

InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);

// Get the string that is to be checked to
// see if it is a palindrome.
System.out.println("Palindrome Checker...");
try
{
System.out.print("Enter a string to check: ");
inStr = console.readLine();
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}

// Create a new StringBuffer object containing the
// same characters as inStr and make revStr
// refer to the new object.
revStr = new StringBuffer(inStr);

// Use the reverse() mutator method of the StringBuffer
// class on the StringBuffer object refered to by revStr.
revStr.reverse();

// Check to see if the inStr and revStr are the same.
// This requires using the .equals() or .equalsIgnoreCase()
// accessor method of the String class. These methods accepts
// a String so the StringBuffer refered to by revSrt must be
// converted to a String...
if (inStr.equalsIgnoreCase(revStr.toString())) {
System.out.println(inStr + " is a palindrome.");
}
else {
System.out.println(inStr + " is not a palindrome.");
}
}
}


  • 0

#4
chancit2003

chancit2003

    Member

  • Topic Starter
  • Member
  • PipPip
  • 15 posts
Sorry,
I wanted to say thank you so much for your assistance. I am struggling with this Java Class I have only worked with one other compiler and well that was a while back.

You will probably see me back...I just love this site and the help is so great.

Thanks Again
Kim
  • 0

#5
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Glad we have been able to help. Feel free to post any programming questions and we will do our best to help out.

Cheers,
Tom

Sorry,
I wanted to say thank you so much for your assistance. I am struggling with this Java Class I have only worked with one other compiler and well that was a while back.

You will probably see me back...I just love this site and the help is so great.

Thanks Again
Kim


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP