0

I have 3 text files named matrix.txt, matrix1.txt, matrix2.txt. I should get a different output since the text file will change every time the program runs. How to do? Appreciating your help.

This is what I was doing:

int min = 1;
    int max = 2;
    int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
    File file = new File("matrix" + randomNum + ".txt");
    if(file.toString().equals("matrix1.txt")) {
        System.out.println("Input filename:" + "matrix1.txt");
    }
    else if(file.toString().equals("matrix2.txt")) {
        System.out.println("Input filename:" + "matrix2.txt");
    }else{
        System.out.println("Input filename:" + "matrix.txt");
        file = new File("matrix.txt");
    }
    
    
    
    Scanner scnr = new Scanner(file);
    int row = 0;
    int col = 0;
    while(scnr.hasNextLine()) {
        String line = scnr.nextLine();
        row++;
    }
    scnr = new Scanner(file);
    String sn = scnr.nextLine();

it goes like that ...... But it keeps reading the else condition, not even read the other if conditions.

Codrunnr
  • 1
  • 3
  • 4
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Stultuske Nov 03 '22 at 07:15
  • You should understand that Java isn't JavaScript. Comparison of the values of objects is done an entirely different way. – Stultuske Nov 03 '22 at 07:15
  • Also: when getting into issues like this, debugging would have shown you that file.toString() == "matrix1.txt" returned false, even if your input was "matrix1.txt" – Stultuske Nov 03 '22 at 07:17
  • @Stultuske no it didn't work. Yes, it returns false but how to fix it? – Codrunnr Nov 03 '22 at 07:20
  • by not comparing the Strings using '==' -> which is a referential comparsion, instead of a comparison of value. If using .equals(..) still returns false, your input was neither "matrix1.txt" or "matrix2.txt" – Stultuske Nov 03 '22 at 07:28
  • @Stultuske didn't work because naturally, it doesn't read matrix.txt file and the values inside of the text file, I don't know why. – Codrunnr Nov 03 '22 at 07:39
  • what is the input you provide? I'm not saying it doesn't read matrix.txt, what I'm saying is that unless you input matrix1.txt, "matrix1.txt".equals(input) will return false. – Stultuske Nov 03 '22 at 07:43
  • @Stultuske this is my homework, so the system gives the input. I just need to read the files and the system chooses between these 3 files and every time the program runs, it tests the other text file, I mean one by one. – Codrunnr Nov 03 '22 at 07:46
  • you still don't tell us what the input is, though. either debug your code, or add a print statement to figure it out, and also show us how you tried to implement the usage of equals – Stultuske Nov 03 '22 at 07:48
  • @Stultuske the assignment is about spiral matrix, I have completed the code part. 3 0 2 6 8, 5 0 -1 -1 1, 5 2 4 9 7 so this is matrix.txt – Codrunnr Nov 03 '22 at 07:50
  • that comment means nothing to us since we don't know your assignment. But, you arrive in the if of matrix1.txt (according to your comment on the answer). Since you only wrote the code to print that line, and nothing to actually handle a file, it's normal it doesn't handle a file. – Stultuske Nov 03 '22 at 07:52
  • You read the file after your if's, nice, but you never actually store it's content, or print it, what do you expect will happen? – Stultuske Nov 03 '22 at 08:00

1 Answers1

0

This is because you can't compare strings with the == operator. You have to use equals(). == checks if both objects point to the same memory location whereas equals() simply compares the values in the objects.

Lena
  • 16
  • 3
  • 1
    You can compare them using ==, and in some scenario's that is what you want, it's just not what he wants here. – Stultuske Nov 03 '22 at 07:36
  • @Lena I have tried equals() too but this time it doesn't read the matrix.txt file and also, the values inside of the text file. – Codrunnr Nov 03 '22 at 07:37
  • @Codrunnr Another question? Then create another one.. – Jean-Baptiste Yunès Nov 03 '22 at 07:39
  • @Jean-BaptisteYunès you mean initializing the new 3 files? I have tried that too but then how do I know when the program runs which text file is being used as output. And, I need to print this one, how? -> System.out.println("Input filename:matrix.txt"); – Codrunnr Nov 03 '22 at 07:43
  • @Codrunnr show how you tried equals, and show what input you provide. – Stultuske Nov 03 '22 at 07:44
  • int min = 1; int max = 2; int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); File file = new File("matrix" + randomNum + ".txt"); if(file.toString().equals("matrix1.txt")) { System.out.println("Input filename:" + "matrix1.txt"); }else if(file.toString().equals("matrix2.txt")) { System.out.println("Input filename:" + "matrix2.txt"); }else{ System.out.println("Input filename:" + "matrix.txt"); file = new File("matrix.txt"); } – Codrunnr Nov 03 '22 at 07:47
  • @Stultuske Input filename:matrix1.txt , and it doesn't read the text file it just prints the file name, and as I have said it doesn't read matrix.txt file – Codrunnr Nov 03 '22 at 07:48
  • that is not your input, that is a static print. Seeing as you never implemented the code to read that file, I think it is normal it is not being read, don't you? – Stultuske Nov 03 '22 at 07:51
  • @Stultuske I have scanned the file, I'm editing the question and you can see. – Codrunnr Nov 03 '22 at 07:53
  • @Stultuske I can separately read the files and then print the values inside of the text file, it works, it prints the values as I want. But what I try to do is, since the program chooses which text file to read, I need an if else condition. If the file name is, for instance, matrix.txt then update the file's name as matrix.txt and the code will do the rest. My if else conditions are not right. – Codrunnr Nov 03 '22 at 08:03
  • again: have you printed the values you are comparing? have you checked that they go in the right field? your if-else don't contain business logic, so it's normal they don't do much. – Stultuske Nov 03 '22 at 08:18
  • @Codrunnr you first have to get the filename. Now you trying to read the file itself and comparing it with the "matrix.txt" string. – Lena Nov 03 '22 at 09:31