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.


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

Results 1 to 3 of 3

Thread: Little String Problem

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Thanked 2 Times in 2 Posts

    Default Little String Problem

    Hey, I've just started messing around with reading from text documents and I've done fairly well in my experiments so-far; I just ran into a small problem with the one part of the code that I was sure would work. When running the program everything runs perfectly except for my if statements. Every single character, which is either a 1 or a 0, is always printing ""+charText+" is null." when I run the program. I haven't done much with strings/char variables before so I'm probably making some simple mistake, if anyone can enlighten me please do so.

    import java.io.*;
    class MapReaderTest
    	public static void main (String args[]) throws IOException
    		String lineText;
    		char charText;
    		int lineCounter =0;
    		int characterCounter = 0;
    		BufferedReader inputStream = new BufferedReader(new FileReader("Map.txt"));
    		while( (lineText = inputStream.readLine()) != null) //While the current line doesn't have nothingon it then keep looping
    			System.out.println(""+lineText+" is the current line.");
    			for(int i = 0; i < lineText.length(); i++) //Loops through each character in the string until the string ends.
    				charText = lineText.charAt(i);
    				if(charText == 0)
    					System.out.println(""+charText+" is a zero.");
    				else if(charText == 1)
    					System.out.println(""+charText+" is a one.");
    					System.out.println(""+charText+" is null.");


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: Little String Problem

    charText is a variable of type char, so it won't ever be null - rather it should have some numeric value.

    The output from
    System.out.println(""+charText+" is null.");

    should have told you what that value is for the two characters. If the char values of the zero and one characters are not clear to you from the program's output, post that output.

    The two char values you test for (0 an 1) are very likely not the values associated with the zero an one characters. Rather than try and commit to memory all the values we use character literal notation:

    if(charText == '0') // '0' is a character literal
        // this code will be executed when charText is the zero character.

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

    tyeeeee1 (January 9th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,274 Times in 2,245 Posts

    Default Re: Little String Problem

    To see the difference between: 0 and '0' use a cast on the char value when you print it:
    Also look at a table of ASCII character values.

    As pbrockway2 said, you should always code char literals: '0' vs their int value: 48
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Line string problem
    By ridg18 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 1st, 2012, 05:30 AM
  2. [SOLVED] String problem
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2011, 07:49 PM
  3. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  4. string tabbing problem
    By rhalliwell1 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 9th, 2011, 12:47 PM
  5. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM