In: Computer Science
I'm getting this error:
Exception in thread "main" java.lang.NoSuchMethodError: main
I tried using public static void main(String[] args){ but that negates all of the methods that I try to write.
I'm just trying to make it so that I can enter values. Thanks.
Code below:
import java.util.Scanner;
public class DataSet2 {
private double value;
private double sum;
private int count;
public void add(double value){
System.out.println("Enter values, enter -1 to finish");
Scanner scan = new Scanner(System.in);
value = scan.nextDouble();
while (value !=-1){
sum =+ value;
if(count<=0){System.out.println("Sorry, that value is not accepted");}
}
}
public void getAverage(){
while (sum >0){
count++;
double average = sum/count;
System.out.println("The average is: " + average);
}
}
public void getLargest(){
double largest =0;
if (value > largest)
largest = value;
System.out.println("The largest is: " + largest);
}
public void getSmallest(){
double smallest =0;
if (value<smallest)
smallest= value;
System.out.println("The smallest is: " + smallest);
}
}
import java.util.Scanner;
public class DataSet2 {
private double value;
private double sum;
private int count;
public void add(double value){
System.out.println("Enter values, enter -1 to finish");
Scanner scan = new Scanner(System.in);
value = scan.nextDouble();
while (value !=-1){
sum =+ value;
if(count<=0){System.out.println("Sorry, that value is not accepted");}
}
}
public void getAverage(){
while (sum >0){
count++;
double average = sum/count;
System.out.println("The average is: " + average);
}
}
public void getLargest(){
double largest =0;
if (value > largest)
largest = value;
System.out.println("The largest is: " + largest);
}
public void getSmallest(){
double smallest =0;
if (value<smallest)
smallest= value;
System.out.println("The smallest is: " + smallest);
}
}
Hi,
I have updated the code to read the values and highlighted the code changes below
DataSet2.java
import java.util.Scanner;
public class DataSet2 {
private double value;
private double sum=0;
private int count = 0;
public static void main(String[] args){
DataSet2 d = new DataSet2();
d.add();
d.getAverage();
}
public void add(){
System.out.println("Enter values, enter -1 to finish");
Scanner scan = new Scanner(System.in);
value = scan.nextDouble();
while (value !=-1){
sum = sum + value;
count++;
value = scan.nextDouble();
}
}
public void getAverage(){
double average = sum/count;
System.out.println("The average is: " + average);
}
public void getLargest(){
double largest =0;
if (value > largest)
largest = value;
System.out.println("The largest is: " + largest);
}
public void getSmallest(){
double smallest =0;
if (value<smallest)
smallest= value;
System.out.println("The smallest is: " + smallest);
}
}
Output:
Enter values, enter -1 to finish
1
2
3
4
-1
The average is: 2.5