Question

In: Computer Science

I'm getting this error: Exception in thread "main" java.lang.NoSuchMethodError: main I tried using public static void...

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);

}

}

Solutions

Expert Solution

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


Related Solutions

Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds...
Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at HW3.main(HW3.java:6) The code: import java.io.FileWriter; import java.io.IOException; public class HW3 { public static void main(String[] args) throws IOException { // 0th argument contains the name of algorithm String algo = args[0]; // 1st argument contains the name of file // Make a new file FileWriter fw = new FileWriter(args[1]); if (algo.equals("p1")) { // 2nd argument comes in the form of...
I keep get this exception error Exception in thread "main" java.lang.NullPointerException    at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)    at...
I keep get this exception error Exception in thread "main" java.lang.NullPointerException    at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)    at Quadrilateral.toString(Quadrilateral.java:51)    at tester1.main(tester1.java:39) In this program I needed to make a Point class to create a coordinate square from x and y. I also needed to make a Quadrilateral class that has an instance reference variable to Point .The Quadrilateral class then inherits itself to other classes or in this case other shapes like square, trapazoid. I thought I did it right but...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
public static void main(String[] args) {            //Part1: using three stacks           ...
public static void main(String[] args) {            //Part1: using three stacks            iQueue<Integer> main = new ourLinkedList<Integer>();                       for (int i = 0; i<10; i++) {                int num = -15 + (int) (Math.random() * ((15 - (-15)) + 1));            main.add(num);            }                       System.out.println("main: " +main);            iQueue<Integer> Q1 = new ourLinkedList<Integer>();           ...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square shade = new Square(getside()); System.out.println("Your chooses are:"); System.out.println("\nCircle"); System.out.println("Triangle"); System.out.println("Rectangle"); System.out.println("Square"); boolean moreShapes = true; boolean userChoice = true; while(moreShapes) { Scanner shapes = new Scanner (System.in); System.out.println("\nWhat shape do you want:"); String yourShape = shapes.nextLine(); if (userChoice = shade != null) { shade.getSide(); } System.out.println("\nWhat are two size paramters of the shape you choose:"); String yourParameter = shapes.nextLine(); System.out.println("\nYour shape is: " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT