-2

My code, for starters:

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.Locale;
import java.util.Arrays;

// Updated file with my own code

public class TextExcel {

   public static void main(String[] args) {
      //command loop:
      Scanner sc = new Scanner(System.in);
      String userinput;
      String[] arrOfStr;
      userinput = sc.nextLine();
      Map<String, String> cells = new HashMap<String,String>();
      while(!(userinput == "quit")) {
         if(userinput.contains("=")) {
            arrOfStr = userinput.split("="); 
            cells.put(arrOfStr[0], arrOfStr[1]);
         }
         if(userinput.contains("cell")) {
            arrOfStr = userinput.split(" ");
            cells.get(arrOfStr[1]);
            System.out.println(cells.get(arrOfStr[1]));
         }
         userinput = sc.nextLine();
      }
      System.exit(0);
   }
}

When I type the following into the console, the following error appears:

 ----jGRASP exec: java TextExcel
A1 = hi
cell A1
null
quit
quit
quit
>:(

 ----jGRASP: process ended by user.

While the "quit" not functioning is not my main problem, I would like help figuring that one out as well. What can I do to make the values callable?

PS I did do the debugging on the Hashmap, and both the value and the key are recorded, but for some reason calling the name returns null.

Ampuja Kid
  • 21
  • 7
  • 1
    What is the value of `arrOfStr[1]` in your second if statement? What is the value of the key you stored in the first if statement? – Sotirios Delimanolis May 17 '20 at 16:56
  • 4
    `split("=")` turns `"A1 = hi"` into `["A1 ", " hi"]` *(notice the spaces)*, and `split(" ")` turns `"cell A1"` into `["cell", "A1"]`. `"A1 "` and `"A1"` are two different values. You can fix it by either calling `trim()` or by including spaces in the split's regex pattern. – Andreas May 17 '20 at 16:56
  • @Andreas so how would I change the Key in my first if statement? – Ampuja Kid May 17 '20 at 16:59
  • 4
    Entering `quit` does work because you need to learn [how to compare strings in Java](https://stackoverflow.com/q/513832/5221149). – Andreas May 17 '20 at 16:59
  • 1
    What happens if I put in `"A1 = cell"`? Don't use magic values or magic strings! – Maarten Bodewes May 17 '20 at 16:59
  • @MaartenBodewes i updated my code to use else ifs, ordering the if contains equals before the if contains cell and then finally if contains quit (to fix the issue). In order to fix the problem I was having, I added a space after arrOfStr[1]. – Ampuja Kid May 17 '20 at 19:26
  • 1
    I'm not in favor of that last fix. In the end you don't want to compare whitespace. *Trimming* the white space around the text is usually the better option. – Maarten Bodewes May 17 '20 at 20:58

1 Answers1

0

split("=") turns "A1 = hi" into ["A1 ", " hi"] (notice the spaces), and split(" ") turns "cell A1" into ["cell", "A1"]. "A1 " and "A1" are two different values. You can fix it by either calling trim() or by including spaces in the split's regex pattern.

Answer given by Andreas! Thank you mate.

Ampuja Kid
  • 21
  • 7