Results 1 to 13 of 13
  1. #1
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default if anyone could help. what am i doing wrong?

    im trying to see if the character is in the string. it is a problem from a book im doing but i seem not to be able to find the answer anywhere :(


    here

    Java Code:
    
    import java.util.Scanner;
    
    public class LetterCounter
    
    {
    	public static void main(String[] args)
    	{
    		String name;
    		String character;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		name = keyboard.nextLine();
    		character = keyboard.nextLine();
    		
    		if (name.indexOf(character))
    			System.out.println("works");
    			
    	}
    }
    :confused:

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Does this compile? If not, and you don't understand what the compiler is getting at, post the exact and entire message.

  3. #3
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    this is what im getting when i try to compile -

    LetterCounter.java:23: incompatible types
    found : int
    required: boolean
    if (name.indexOf(characters))
    ^


    ahh i see what im doing wrong. I need a boolean, but how do i substitute? how can i correct it?
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  4. #4
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    public int indexOf(int ch):
    1. returns int, not boolean.
    2. gets int, not string.

  5. #5
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by codesmuggler View Post
    public int indexOf(int ch):
    1. returns int, not boolean.
    2. gets int, not string.


    but int cant be a character can it?
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    ahh i see what im doing wrong. I need a boolean, but how do i substitute?

    It's no different than say if you were writing a program to check a person's age. Say you had method that returned an int - like indexOf() - and you wanted to see if they were 18. You would write something like this:

    Java Code:
    if(person.getAge() < 18) 
    {
        System.out.println("No Entry!");
    }

    Ie you construct a boolean expression like "person.getAge() < 18" that will be good for the if statement. To see what boolean expression you should use read the indexOf() API docs to see what int value it returns under various conditions including when the target is not in the string.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    but int cant be a character can it?

    Don't worry about this: although you are calling it character it is declared to be a String. Presumably it is a one letter String that the user enters. indexOf() will find it (or not) quite happily.

    -----------------

    (This assumes that you keep using the form of indexOf() that takes a String argument. If you like you could get the first char of the String character and use the other form of indexOf(). In that case you are right: you can use a char where the method wants an int)
    Last edited by pbrockway2; 02-19-2011 at 11:59 PM.

  8. #8
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    im trying my best to understand :( so can i test if a string has the "character" in the string entered by user? if not, what should i use? :confused:
    im sorry, i know im a pain in the butt but im trying my best
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    but im trying my best

    That's OK - we all assume that. (Or we don;t bother to respond ;)

    (1) So have you read the API docs? (2) What int value is returned if the target (character - I don;t know where it picked up that final 's' in your error message...) is not in the String?

    (3) Can you think of a boolean expression so the if statement begins "if(name.indexOf(character) is not equal to <number-you-have-read-that-is-returned-for-no-match>) {"?

  10. #10
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    yea i read it, to be honest i dont get it all, i guess im not functioning properly or something. well as it says in the API docs, it will return -1.
    wait. errrrrrrrr

    if(name.indexOf(character != -1) ? testing it right now

    edit: I hate myself. its not working. i cant use !=
    Last edited by glina126; 02-20-2011 at 12:23 AM.
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    edit: I hate myself. its not working. i cant use !=

    Yes, you can use !=. But you have to use it in the right place.

    Compare what you have with the example I posted before: (adjusted)

    Java Code:
    if(person.getMark() != 666)      // previously - if(person.getAge() < 18)
    {
        System.out.println("Welcome, friend!");
    }

  12. #12
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    wow that took me a while to get! thank you for being so patient! :)

    here is my final code that WORKS!

    Java Code:
    import java.util.Scanner;
    
    public class LetterCounter
    
    {
    	public static void main(String[] args)
    	{
    		String name;
    		String character;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		name = keyboard.nextLine();
    		character = keyboard.nextLine();
    		
    		if (name.indexOf(character) != -1)
    			System.out.println("works");
    			
    	}
    }

    now time to go on another journey and figure out how to display how many times that character appears in the string xD

    again, thanks soooooo much!
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You're welcome.

    Take your time reading those API docs. The style is dense and favours logical precision and completeness over friendliness, but they are well worth the effort. I mention this because indexOf() comes in yet another flavour that will prove most helpful in counting occurances.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. what am i doing wrong here?
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-21-2010, 05:04 PM
  3. what's wrong?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 04-14-2009, 10:07 PM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 12:45 PM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •