Results 1 to 5 of 5
  1. #1
    Lord ice is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Question Convert from string to double

    Okay so im tring to convert a string to a double, but i keep geting a error saying: incompadible types found double but expected java.lang.string

    i already tried switching things around but i cant get it to work.
    this is the peice of code that im useing:

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Ice_Cream_Menu
    {
        public static void main(String args[]) throws IOException
        {
            System.out.print("\f");
    
            String X = "";         
            String filename;
    
            filename = "Prices.txt";
            File file = new File(filename);
    
            Scanner inputFile = new Scanner(file);
            Scanner keyboard = new Scanner(System.in);
            
            final int total = 30;
            double prices[] = new double[total];  
            String product[] = new String[total];  
            
            for(int r=0; r<total; r++)
            {
                product[r] = inputFile.nextLine();  
                X = inputFile.nextLine();           
                product[r] = Double.parseDouble(X);   // This is were im haveing the error.
            }
            inputFile.close();
    This is the piece that i keep geting the error:

    Java Code:
            for(int r=0; r<total; r++)
            {
                product[r] = inputFile.nextLine();  
                X = inputFile.nextLine();           
                product[r] = Double.parseDouble(X);   // This is were im haveing the error.
            }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Lord ice is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please post the actual error message. Learning to understand these is paramount.
    This is the error im haveing:
    incompatible type - found double but expected java.lang.String

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You declared product as a String[], and you are trying to assign a double to product[r]. My guess is you meant to assign it to prices[r].

    -Gary-

  5. #5
    Lord ice is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    You declared product as a String[], and you are trying to assign a double to product[r]. My guess is you meant to assign it to prices[r].

    -Gary-
    THANKS!! I totaly missed that.

Similar Threads

  1. How to convert a double into a int?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 10:02 AM
  2. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM
  3. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  4. Replies: 2
    Last Post: 11-16-2007, 01:24 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •