Results 1 to 3 of 3
  1. #1
    CurtisJC is offline Member
    Join Date
    Mar 2009
    Location
    England, London
    Posts
    1
    Rep Power
    0

    Default basic help needed

    Hey all, this is my first post, i've been using Java for about 5 months now and I'm still pretty new to programming let alone the language.

    I need some help with some code:

    import java.util.Scanner;

    public class HangMan
    {
    public static void main (String[] args)
    {
    Scanner sc = new Scanner(System.in);
    String strDash = "__________";
    String strWord = "helloworld";

    while(true)
    {
    System.out.println(strDash);
    System.out.println("Pick a letter");
    String s = sc.next();
    char c = s.charAt(0);

    for(int i = 0;i < strWord.length() - 1; i++)
    {
    if(strWord.charAt(i) == c)
    {
    strDash.charAt(i) = c;
    }
    }
    }
    }
    }

    I'm trying to change the letters in strDash if they match with the users input compared to strWord and getting:

    HangMan.java:30: unexpected type
    required: variable
    found : value
    strDash.charAt(i) = c;
    ^
    1 error

    No idea what's wrong with that line and i think it must be a simple answer but just can't figure it out, thanks in advance for any help you can provide :)
    Last edited by CurtisJC; 03-02-2009 at 06:31 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because a String is immutable (it can't be changed). Look at the API docs String, the method replace, and assign the result to a new String.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default To String or not to String ...

    Here's the link to the Java 6 APIs:
    Java Platform SE 6

    Here's the link to the particular String replace() method:
    String Replace ()

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 06:17 PM
  2. Basic Help
    By robjames in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:58 AM
  3. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 06:00 AM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 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
  •