Computer Science Canada

Cannot find symbol method readDouble

Author:  Srlancelot39 [ Mon Sep 17, 2012 10:40 am ]
Post subject:  Cannot find symbol method readDouble

I am trying to write a program that gets 5 numbers of type double from the user and displays the average.

The error I am currently receiving is:
Lec04.java:13: error: cannot find symbol
nums[i] = readDouble<"Enter a real number, then press Enter: ">;
^
symbol: method readDouble<String>
location: class Lec04

After doing some research, I found that apparently the method needs to be in the class and that the standard library may not contain the method. I am not sure how to fix this though.

Thanks!


The code is as follows:
import java.util.*;
import java.io.*;

public class Lec04
{
public static void main(String[] args)
{
System.out.println("Hello World!");
double[] nums = new double[5];
for(int i = 0; i < 5; i++)
{
System.out.println("Number " + (i+1) + ":");
nums[i] = readDouble("Enter a real number, then press Enter: ");
}
double avg = (nums[1] + nums [2] + nums [3] + nums [4] + nums [5])/5;
System.out.println(avg);
}
}

Author:  matt271 [ Mon Sep 17, 2012 11:36 am ]
Post subject:  Re: Cannot find symbol method readDouble

It looks like your course requires some JAR file. Find this JAR file and place it in the same directory as your source code. If you are not using packages and compiling by running javac Lec04.java and running by running java Lec04 this should work.

This might be what you need: http://introcs.cs.princeton.edu/java/stdlib/
And it looks like the readDouble method does not take a String as an agrument. So you will have to change your code slightly to something more like this:
StdOut.println("Enter blah blab:");
double bleh = StdIn.readDouble();

Author:  Srlancelot39 [ Mon Sep 17, 2012 11:41 am ]
Post subject:  RE:Cannot find symbol method readDouble

Okay thanks, I'll try that!
and yeah you're correct, i am using javac and java in cmd.

Author:  Srlancelot39 [ Mon Sep 17, 2012 1:47 pm ]
Post subject:  Re: Cannot find symbol method readDouble

Thanks again! That was what I needed.

Here is the working code:

code:
import java.util.*;
import java.io.*;

public class Lec04
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
                double[] nums = new double[5];
                for(int i = 0; i < 5; i++)
                {
                        System.out.println("Number " + (i+1) + ":");
                        System.out.println("Enter a real number: ");
                        nums[i] = StdIn.readDouble();
                }
                double avg = (nums[0] + nums [1] + nums [2] + nums [3] + nums [4])/5;
                System.out.println("The average is: " + avg);
        }
}


: