1

EDITED to remove excess code and show the exact errors I am receiving.

I am attempting to show my array in a grid format (like a spreadsheet). I have declared it an array of strings, so that I can use readLine and split however 6 out of 7 columns are doubles. I keep getting incompatible types errors on all of the array variables. More specifically: Incompatible types: int cannot be converted to string, incomparable types int and string and incompatible types string cannot be converted to double I am new and when I researched on here, I couldn't find how to fix this. Here is my code:

         while(s != null) {
          array = s.split(delimiter);
          dates = array[0];
          dates = Integer.parseInt(array[0]);
          double[] nums = new double[array.length];
          for (int i = 0; i < nums.length; i++) {
          nums[i] = Double.parseDouble(array[i]);
          }       
          if(dates != 0)  {
            open = array[1];
            high = array[2];
            low = array[3];
            close = array[4];
            adjClose = array[5];
            volume = array[6];
6
  • Please provide the exact error message and remove those bits of code which are not directly relevant to the said error. Commented Jul 25, 2017 at 22:21
  • open = Double.parseDouble(array[1]); Commented Jul 25, 2017 at 22:24
  • Why assign something the to dates variable (dates = array[0]), then immediately replace it with something else (dates = Integer.parseInt(array[0]))? If text stored in array[0] is supposed to be an integer number, why is dates type String, and not type int? If you know that array[0] is not a double value, why does your loop calling parseDouble() start at index 0? If you already parsed all the values to double, why are you trying to assign array[1] to open, not nums[1]? Same question for indexes 2-6? Commented Jul 25, 2017 at 22:31
  • A String doesn't become a double just because you've assigned a String value to a double variable. Commented Jul 26, 2017 at 1:15
  • I thought that I converted it to a double with Double.parseDouble? Commented Jul 26, 2017 at 2:07

1 Answer 1

4

Java is a statically typed language. You can't just take a string and pretend it's a double. A String[] variable is always going to contain strings. It might contain strings that look like doubles, but they will always be strings.

String[] array = ...;
double x = array[0];

This is a type error because we're extracting a string from the array and asking the system to simply pretend it's a double, which it won't do.

String[] array = ...;
double x = Double.parseDouble(array[0]);

This will convert the string value to a double. If the data you're getting is from a trusted source (for instance, if it's for a class and your teacher promised the data would be valid), then that's fine. But if the data is coming from a user or other source, Double.parseDouble will throw an exception if the data is invalid, and you need to think about the correct way to handle that in your particular case.

For the sake of completeness, here is the opposite conversion.

String string = x.toString();

Any object (and, through a trick of the Java compiler, any primitive as well) can have toString called on it, and this will turn the object into a string.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, most 'raw' types (int, double, boolean, etc.) don't have a .toString() method that you can call directly (although it does get handled sometimes behind-the-scenes.) However, their related Object class versions should have a static method that can be called: Integer.toString(someInt), Double.toString(someDouble), etc.
I think you can use String.valueOf to convert those raw types to string if needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.