Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Comparing a character to an array of strings

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Comparing a character to an array of strings

    Hi everyone, this is my first post on this forum. I'm having trouble with a program assigned for homework. I would say that I am at a beginners level in java. Here's my code in class Bug:

    String[][] pathInfo = new String[5][25];

    ........;
    ........;

    /*
    Prints array for testing/debug use
    @return pathInfo 2D array
    */
    public void printArray(String[][] pathInfo)
    {
    for (int row = 0; row < 5; row++)
    {
    for (int column = 0; column < 25; column++)
    {
    if (pathInfo[row][column] == "*") !!!!<<<<------I think the problem lies here
    {
    System.out.print(pathInfo[row][column]);
    }
    else
    {}
    }
    }
    }

    public String[][] fillArrays(String[][] pathInfo) throws FileNotFoundException
    {
    Scanner bugPathScan = new Scanner(new File("bugpath.txt"));
    bugPathScan.useDelimiter("");
    for (int row = 0; row < pathInfo.length; row++)
    {
    for (int column = 0; column < pathInfo[row].length; column++)
    {
    pathInfo[row][column] = bugPathScan.next();
    }
    }
    return pathInfo;
    }

    It fills the array with info from a txt file. The text file contains a bunch of symbols, and looks something like this:

    ****-*--*
    *-----****
    *-----*--* <--my goal is to print only the '*' characters and skip the '-'
    ****-*--*


    How could I compare the value at the current position within the array to the "*" character? I've tried changing the array type to char[][] but I can't use the scanner to read in a char value. Any ideas? Your help is greatly appreciated


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Comparing a character to an array of strings

    .equals() not ==

    I think..

  3. The Following User Says Thank You to ppme For This Useful Post:

    Hashmeer169 (December 4th, 2011)

  4. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Comparing a character to an array of strings

    An extension of what ppme said:

    When comparing two string literals, it is best to use the .equals() method to compare if each char is the same, rather than the == comparison operator that will check if both objects are the same.

    However if you want to compare chars, use single quotation marks. EG
    /*
     * Precondition: c1 is a char.
     */
    if(c1 == 'a')
    {
      //Not included
    }

    For getting a specific char at an index in a String, look at the charAt method in the String API:

    String (Java Platform SE 6)
    Last edited by Tjstretch; December 4th, 2011 at 12:13 PM. Reason: Can't spell charAt *fails*

  5. The Following User Says Thank You to Tjstretch For This Useful Post:

    Hashmeer169 (December 4th, 2011)

  6. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Comparing a character to an array of strings

    Thanks for the tip! Using the .equals method "if (pathInfo[row][column].equals('*'))" lets it compile but its still not printing anything. I'm not sure where the problem lies now though :/

  7. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Comparing a character to an array of strings

    No problem, however string literals aren't chars.

    pathInfo[row][column] is a string, but '*' is a char, so you are comparing a String to a char, which I'm surprised compiles.

    Double quotes(") makes "*" a string, which you can use to compare to pathInfo.

  8. The Following User Says Thank You to Tjstretch For This Useful Post:

    Hashmeer169 (December 4th, 2011)

Similar Threads

  1. get vowel character from strings
    By siddiqui_1985 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2011, 04:30 AM
  2. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  3. [SOLVED] if statement incorrectly comparing strings
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2011, 08:59 PM
  4. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM
  5. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM