Results 1 to 7 of 7

Thread: chars

  1. #1
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default chars

    How do i do an if statement about chars?

    as if...

    Java Code:
    import java.util.Scanner;
    
    public class chars
    {
      public static void main( String args[] )
      {
        String name1;
        String name2;
        int points1;
        int points2;
        
        Scanner input = new Scanner(System.in)
    
        System.out.println( "What is your name?" );
        name1 = input.next();
    
    [B]    if ( name1 = Jerry )[/B]
        {
          points1 = points1 + 1;
        }
     
        System.out.println( "Whats your opponents name?" )
        name2 = input.next();
    
    [B]    if ( name2 = CPU )[/B]
        {
          points2 = points2 + 1;
        }
      }
    }
    The if statements are what is annoying me.
    I cant make it so it'll take the char....

    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried something like:
    Java Code:
    if (name1.equals("Jerry"))
    {
      //....
    }

  3. #3
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    oh darn, srry i tried that but forgot the () around jerry, but shouldn't there be another way to do that?

  4. #4
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    also how would i do a defult, like make the defult Jerry so the user would have to delete the chars?

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by whosadork View Post
    How do i do an if statement about chars?

    [CODE]
    if ( name1 = Jerry )
    {
    points1 = points1 + 1;
    }
    Don't you mean to use == rather than =?
    Even C doesn't do the comparison with a single equal

  6. #6
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

    Default

    you cant compare strings with ==
    only characters.
    use compareTo or equals method.

  7. #7
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

Similar Threads

  1. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 03:20 PM
  2. Can we append more than 255 chars to <a href>??
    By freddieMaize in forum Advanced Java
    Replies: 22
    Last Post: 07-18-2008, 05:04 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
  •