Need help with user imput
Im trying to make a program that does a formula and the user imputs the variables and it prints the result.
I dont know terminology, so im just gona call this the class with the formula
Code java:
//Geometeric Series - Sn = (1-r^(n-1))/(1-r)
public class geometricSeries
{
public static double geometricSeries(double a, int n, double r)
{
double sum = (1-(Math.pow(r,n)))/(1-r);
return sum;
}
}
This is the user imput part i have a problem with
Code java:
import java.util.Scanner;
public class geometricSeriesDriver
{
public static void main(String[]args);
{
System.out.println("Imput the first term");
Scanner scan = new Scanner(System.in);
double a = scan.nextDouble;
System.out.println("Imput the n term");
int n = scan.nextInt;
System.out.prinln("Imput the ratio");
double r = scan.nextDouble;
return sum;
}
}
I have no idea how to connect the two, its been like 2 weeks since ive written code and im just starting so i kind of forgot how. Also i get these errors.
geometricSeriesDriver.java:5: error: missing method body, or declare abstract
public static void main(String[]args);
^
geometricSeriesDriver.java:10: error: cannot find symbol
double a = scan.nextDouble;
^
symbol: variable nextDouble
location: variable scan of type Scanner
geometricSeriesDriver.java:13: error: cannot find symbol
int n = scan.nextInt;
^
symbol: variable nextInt
location: variable scan of type Scanner
geometricSeriesDriver.java:15: error: cannot find symbol
System.out.prinln("Imput the ratio");
^
symbol: method prinln(String)
location: variable out of type PrintStream
geometricSeriesDriver.java:16: error: cannot find symbol
double r = scan.nextDouble;
^
symbol: variable nextDouble
location: variable scan of type Scanner
geometricSeriesDriver.java:18: error: return outside method
return sum;
^
6 errors
Re: Need help with user imput
errors could be fixed by changing scan.nextDouble; to scan.nextDouble(); and scan.nextInt; to scan.nextInt();
There is no method of prinln(), I think it should be println();
Re: Need help with user imput
Thank you, changing to scan.nextDouble() and scan.nextInt() cleared up 3 errors. And the prinln was changed to println();
I still get these errors for the second one
geometricSeriesDriver.java:5: error: missing method body, or declare abstract
public static void main(String[]args);
^
geometricSeriesDriver.java:18: error: return outside method
return sum;
2 errors
Re: Need help with user imput
Read those errors carefully, as they describe the problem pretty explicitly. For instance:
Quote:
missing method body, or declare abstract
main is a method...where is its body? If you don't know what this means, see
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
and
Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)
Re: Need help with user imput
Why not just move the method into the geometricSeriesDriver class?
Like so;
Code :
public class geometricSeriesDriver
{
public static double geometricSeries(double a, int n, double r)
{
double sum = (1-(Math.pow(r,n)))/(1-r);
return sum;
}
public static void main(String[]args);
{
System.out.println("Imput the first term");
Scanner scan = new Scanner(System.in);
double a = scan.nextDouble;
System.out.println("Imput the n term");
int n = scan.nextInt;
System.out.prinln("Imput the ratio");
double r = scan.nextDouble;
return sum;
}
}
As the other user mentioned, you need add a () to nextInt and nextDouble to become nextInt() nextDouble()
also you never defined the variable sum. so it would be: double sum;
do you know how to call a method?
Re: Need help with user imput
Quote:
Originally Posted by
clydefrog
Like so;
Please read the compile messages posted in post #3 above - your code just recapitulates the errors.
Re: Need help with user imput
Quote:
Originally Posted by
copeg
Please read the compile messages posted in post #3 above - your code just recapitulates the errors.
I just want to know why he/she has a separate class when you could put the whole thing in just one class. I wasnt trying to debug those errors; though i have fixed the code and runs on my machine lol.
I want to see if the OP has figured it out