简体   繁体   中英

How to compare read byte from file to single character string

I have a text file where I need to check if there is a "1" or a "0" at a certain position. Note that it's the string representation of the numbers I'm checking. I've tried this:

RandomAccessFile dictionaryFile = new RandomAccessFile("path");
if("1".equals(new String(dictionaryFile.read()))){
    // do stuff
}

but it results in:

ir/PersistentHashedIndex.java:322: error: no suitable constructor found for String(int)
        while("1".equals(new String(dictionaryFile.read()))){
                         ^
constructor String.String(String) is not applicable
  (argument mismatch; int cannot be converted to String)
constructor String.String(char[]) is not applicable
  (argument mismatch; int cannot be converted to char[])
constructor String.String(byte[]) is not applicable
  (argument mismatch; int cannot be converted to byte[])
constructor String.String(StringBuffer) is not applicable
  (argument mismatch; int cannot be converted to StringBuffer)
constructor String.String(StringBuilder) is not applicable
  (argument mismatch; int cannot be converted to StringBuilder)

Seems to me like String needs a byte array instead of just a byte to initialise a string. But I only want to give it one number. How can I achieve this? Can I maybe convert "1" to its byte representation?

EDIT: Sorry about the comparison mistake, guys, I sorted it out. Same problem persists.

no suitable constructor found for String(int)

It means you are passing integer value to String constructor whereas, there isn't any constructor for String , which accepts int value. If dictionaryFile.read() return int . Then, you could do

if (dictionaryFile.read() == 1)
{

==Edited==

If you are forced to compare as String, then you just add empty string to it.

String temp = dictionaryFile.read()+"";

if ("1".equals(temp))
{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM