-2

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.

Sahand
  • 7,980
  • 23
  • 69
  • 137
  • That's not the way to compare Strings even if that did work. – Kayaman Feb 05 '18 at 18:42
  • Please read [how to compare Strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Juan Carlos Mendoza Feb 05 '18 at 18:42
  • 1
    what is `dictionaryFile.read()` ?? – Ravi Feb 05 '18 at 18:44
  • In such cases you should consult the official API (its free, really!) `InputStreamReader.read()` returns an `int` (containing the current bytes value), not a `byte` as you expect but there is no constructor accepting an `int` parameter in `String`... – Timothy Truckle Feb 05 '18 at 18:46

1 Answers1

1

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))
{
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Yes, but I don't want to compare it to integer 1. I want to compare it to "1". Is there really no way to just make a string out of a single byte?? – Sahand Feb 05 '18 at 18:50
  • @Sahand any specific reason ? and what is `dictionaryFile.read()` ? what does it return ? – Ravi Feb 05 '18 at 18:51
  • Because I have my own file format where "1" means the current position is occupied and "0" means not occupied. dictionaryFile.read() is a RandomAccessFile method, I've added the object declaration in the code. – Sahand Feb 05 '18 at 18:52
  • It seems like your solution results in "49" instead of "1" if a string "1" (49 in int value) is read from file. – Sahand Feb 05 '18 at 19:17
  • @Sahand FYI.. my solution only convert int to String, which you asked. It doesn't change the value. – Ravi Feb 05 '18 at 19:19