-3

//CODE SNIPPET

boolean ShouldContinue1 = true;
List<String> b = new LinkedList();
input.useDelimiter("//s");

  




  while (ShouldContinue1) {

       String key = input.nextLine();

       b.add(key);

       int[] num = new int[(b.size()) / 2];
       int[] denom = new int[(b.size()) / 2];

   if (b_contains_string(key) == 1) {

  

// Problem

       for (int i = 0; i < b.size() - 1; i++) {

            if (i % 2 == 0) {

               

                   num[i / 2] = Integer.parseInt(b.get(i));
            
           } 
          else if (i % 2 != 0) {

           

        denom[i / 2] = Integer.parseInt(b.get(i));
        
      }
    }
  } 
  else {

    for (int i = 0; i < b.size(); i++) {

      if (i % 2 == 0) {

        

//This line causes error

            num[i / 2] = Integer.parseInt(b.get(i));

//

      } 
      else if (i % 2 != 0) {

        

             denom[i / 2] = Integer.parseInt(b.get(i));

//Ends

       }
    }

  }

//The error message I get (with specified lines)

//Exception in thread "main" java.lang.NumberFormatException: For input string: "12 24 21 30" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at line

//

//

Not a duplicate, other posts ineffective in explaining my problem

  • 1
    Does this answer your question? [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – OH GOD SPIDERS Dec 06 '22 at 10:47
  • 3
    "So im trying to cast List as int" - no you're not, you're trying to cast a list *element* to `Integer`, and that element is a `String`. – Jon Skeet Dec 06 '22 at 10:47
  • You have String. If you want to convert those to int then you need to convert them. Casting doesn't do any conversion and you can only cast to something the Object already is. eG `Object obj = "hey";` and `(String) obj` would work because the Object already is a String. – OH GOD SPIDERS Dec 06 '22 at 10:48
  • 2
    unrelated: stick to java naming conventions, please .. and put some effort into formatting to make the code easily readable – kleopatra Dec 06 '22 at 10:50
  • This is why you want to use `List = ...`, to get started. And PLEASE read about Java naming conventions - as is, your code is a pain to read :-) – Gyro Gearless Dec 06 '22 at 10:50
  • 3
    Don't use raw types either. Say what your List has. `List b = new LinkedList<>();` or even better, `List b = ...;` then you dont' have to cast, you just have to convert your strings to integers when you put them in the list. – matt Dec 06 '22 at 10:51
  • @Gyro Gearless The list contains both numbers and chars – SantaClausSatan Dec 06 '22 at 11:00
  • Hence I cannot convert them to INT when im adding them to the list – SantaClausSatan Dec 06 '22 at 11:01
  • You need to change this, `List b = new LinkedList();` to `List b = new LinkedList<>();` I'm a bit surprised it compiles. Are you sure you're not just running an old version? – matt Dec 06 '22 at 12:36

2 Answers2

0

you should use the parseInt method for converting string to integer.

Eg: num[i / 2] = Integer.parseInt((b.get(i));

0

List<String> b = new LinkedList(); means that the list b can only store strings.

You're then checking if(b.get(i) instanceof Number){, which won't work because b only contains strings, as it is of type List<String>. You're getting a compile time error because the compiler knows that it will always be false, because the String and Number aren't interchangable like that.

x instanceof SomeType is used to see if x's type is castable to SomeType. So in your code it is checking if the type of b.get(i) is stored as a Number. b.get(i) will always be a string, and so b.get(i) instanceof Number will throw an exception, because regardless of what that string is (whether it is "asdaasd" or "12" or "123asda") it will never be stored as a Number, as the value came from a List<String>.

Remove the instanceof Number if statements as they are causing the errors you are seeing. If you are just expecting numerical strings (like "12", "41", "123" etc) in b, just use the Integer.parseInt. If you are expecting a mixture of numeric strings and non-numeric strings (like "12", "aaa", "123aaa"), then please look at this question: How to check if a String is numeric in Java . Despite what you said in your question, it is effective at explaining your problem.

(Editted for clarity/based off comment below)

John
  • 500
  • 4
  • 15
  • Tried that, didn't work, error: error: incompatible types: String cannot be converted to Integer – SantaClausSatan Dec 06 '22 at 12:14
  • My guess is: I think he's trying to check if the String only contains numbers with this `instanceof Number` check. He said his List might contain "Strings and Numbers" so I would guess that the List can contains numerical and non-numerical Strings. Why he doesn't simply search [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) and seems to keep guessing code is beyond me however. – OH GOD SPIDERS Dec 06 '22 at 12:15
  • 2
    @SantaClausSatan It is getting quite hard to help you because you aren't putting much effort into your question or responses. Please could you edit your question to include expected data (ie. what the contents of `b` is likely to be), a well-formatted version of your code, as well as compilation errors (highlighting the specific lines that are causing the errors). – John Dec 06 '22 at 12:22
  • EXAMPLE: The string will at one point contain {12, 21, 23, 20) and at another point {12, 24, 21, q} – SantaClausSatan Dec 06 '22 at 12:24
  • I've edited the answer to include more information based on your examples. In future please remember that stackoverflow aren't just free programmers for you, and you should put some effort researching your problem and formatting/phrasing your question to include all relevant details. Have a look at https://stackoverflow.com/help/how-to-ask – John Dec 06 '22 at 12:45