Results 1 to 3 of 3
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Rep Power

    Default chars being read from a file not treated like literals


    I'm reading a txt one line at a time and I'm using this (pseudo, I'm hoping I don't have to provide the real code):

    Java Code:
    FileInStream fs = new fs("file");
    Scanner sc = new scan(fs);
    while (sc.hasnextline())
       input = sc.nextline();
       valLength = input.length();
       bitpos = valLength - 1;
       valconv = 0;
       while (bitpos >= 0)
          valtemp = input.substring(bitpos, bitpos + 1)
          power = (double) bitpos;
          if (valtemp != "0")
             valconv = valconv + Math.pow(2, power);
    "valtemp" is a string and so is "input". all of the numbers in the file are bins, like:

    Java Code:
    but the code is telling me that "valtemp" is *always* not 0, so basically always 1. not true. if I check for "1" instead of "not 0", the condition is *never* true. what am I missing here guys?


  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Bali, Indonesia
    Rep Power

    Default Re: chars being read from a file not treated like literals

    When you comparing two strings for equality always use the equals() or equalsIgnoreCase() method. Never use the == or != operator it doesn't work for string equality comparison.
    Last edited by wsaryada; 04-05-2013 at 07:35 AM.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: chars being read from a file not treated like literals

    If you want to use == and actually have your characters treated as literals, you can use the charAt() method of the String class.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Reading chars from a file
    By connorlm3 in forum New To Java
    Replies: 6
    Last Post: 02-12-2013, 11:29 PM
  2. Replies: 2
    Last Post: 07-21-2011, 03:46 AM
  3. Deleting specific chars from a file
    By javauserjava in forum New To Java
    Replies: 10
    Last Post: 04-03-2011, 05:25 PM
  4. Replies: 10
    Last Post: 03-29-2011, 11:09 AM
  5. Replies: 1
    Last Post: 02-07-2011, 09:22 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