Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2007, 09:47 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
Help With Input.charAt(LastIndex);
I have got a question about something that I just cant figure out on my own.
With that said, here is the code:

Code:
import java.util.*; import javax.swing.*; import java.lang.*; public class Prob01BAC { public static void main(String args[]){ String Input, LastChar; int LastIndex; double StringLength; //Gathers Input from user and stores in the 'Input' Variable for later use Input = JOptionPane.showInputDialog("Please enter a sentence"); StringLength = Input.length(); LastIndex = Input.lastIndexOf(Input); LastChar = Input.charAt(LastIndex); //Check to see if the last character of the string is a '?' if ( LastChar == "?" ){ //Checks to see if the string is even or odd by dividing by 2 and checking the remainder //0 = even, anything else is odd if ( StringLength % 2 == 0 ){ JOptionPane.showMessageDialog(null, "Yes"); } else{ JOptionPane.showMessageDialog(null, "No"); } } //Checks to see if the last character of the string is a '!' else if ( LastChar == "!" ){ JOptionPane.showMessageDialog(null, "Wow"); } //This is a sort of catch all for anything that doesnt meet the above requirements else{ JOptionPane.showMessageDialog(null, "You always say \"" + Input + "\""); } } }
When i try and compile it I get this message

Code:
Prob01BAC.java:20: incompatible types Found : char Required : java.lang.String LastChar = Input.charAt(LastIndex);
the first ( has a marker under it).

The object of the program is to take a user input string using the JOptionPane method and then responding with a certain string according to certain variables like the string length being odd or even, the string ending in a ! or a ?, or nothing at all.

I have tried googling the error, but with my limited knowledge I found nothing that appeared to be of any use in the way of resloving the problem.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 05:22 AM
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
The problem is that you are trying to assign a char type to a String type and this is not allowed since these types are incompatible.

You declared LastChar as a String but in your code you do this:

Code:
LastChar = Input.charAt(LastIndex);
As the String.charAt method returns a char, the things go wrong.

Some info about String and char:
- char is a primitive type and represents a single character: 'a', 'D'. Notice that you always use single quotes (') to represent a char literal.

- String is an object which represents a string of characters: "abcd". Notice that you always use double quotes (") to represent a String literal.

So the solution for your problem is to change the LastChar type to char:
Code:
char LastChar;
Furthermore you need to change the lines:
Code:
if ( LastChar == "?" ){
and
Code:
if ( LastChar == "?" ){
To:
Code:
if ( LastChar == '?' ){
and
Code:
if ( LastChar == '?' ){
Pay attention to the single quotes. If you say:

Code:
(LastChar == "?")
The java compiler will interpret "?" as a String with only one character and not as a char.
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cant take input from user new_1 New To Java 6 12-25-2007 08:38 AM
input placed in array smilejava New To Java 5 11-12-2007 08:29 AM
input placed in array smilejava New To Java 1 11-05-2007 01:32 PM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 09:46 AM
How to get input from Console karma New To Java 2 05-20-2007 12:32 PM


All times are GMT +3. The time now is 12:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org