Can anyone help me to make this java run without error? - java

I keep getting error "Error: Could not find or load main class GradesAverage" after trying to compile it.
Can anyone help me understand where it went wrong in this code?
package javaexercises.arrays;
import java.util.Scanner;
public class GradesAverage {
private final int LOWEST_GRADE = 0;
private final int HIGHEST_GRADE = 100;
// student's grades
private int[] grades;
private Scanner in;
/**
* Enter program's point.
*
* #param args
*/
public static void main(String[] args)
{
GradesAverage aGradesAverage = new GradesAverage();
aGradesAverage.in = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numStudents = aGradesAverage.in.nextInt();
aGradesAverage.run(numStudents);
}
/**
* Run program.
*
* #param numStudents
*/
private void run(int numStudents)
{
if (numStudents <= 0) {
System.out.println("Invalid number of students.");
return;
}
grades = new int[numStudents];
double sum = 0;
int i = 0;
while (i < numStudents)
{
System.out.printf("Enter the grade for student %1$d: ", (i+1));
int grade = in.nextInt();
// chek if grade is between 0 and 100
if ((grade >= LOWEST_GRADE) && (grade <= HIGHEST_GRADE)) {
grades[i] = grade;
sum += grade;
i++;
continue;
}
System.out.println("Invalid grade, try again...");
}
System.out.printf("The average is %1$.2f\n", (sum / numStudents));
}
}

Some online compilers don't handle packaging well.
Comment out this line
package javaexercises.arrays;
It should work.

Related

Issue: Reading 10 different marks into my marks variable. FOR loop

import java.util.*;
public class loops
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in).useDelimiter("\n");
for (int i = 0; i <= 9; i++)
{
System.out.print("Enter your mark: ");
int marks = input.nextInt();
}
int marks = + input.nextInt();
int totalmarks = marks / 10;
System.out.println("The class average was:"+ totalmarks + ".");
}
}
The question is not clearly asked but according to my interpretation, the problem is to add all the input marks and give the average, so declare a sum variable with initial value 0 and add all the marks coming as input and take the average.
import java.util.*;
public class loops
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in).useDelimiter("\n");
int sum=0;
for (int i = 0; i <= 9; i++)
{
System.out.print("Enter your mark: ");
int marks = input.nextInt();
sum=sum+marks;
}
int totalmarks = sum / 10;
System.out.println("The class average was:"+ totalmarks + ".");
}
}

Java Array Grading

I was looking for some input on my code. What i'm trying to do is get user input in the form of scores on a test or something. The user can input how many grades they need to enter and it stores them on the array. Then the program will grade on a curve with the highest being an A. if a score is within 10 points of the highest score, it is also an A, if its in between 10 to 20 points less than the higher score its a B, if its 20 to 30 points less than the highest score then its a C, and so on till F.
My issue is that I don't know how to compare a certain element to the highest to see what grade it is. So my question is how do you compare a certain element in an array to the highest element in the array?
import java.util.Scanner;
public class StudentGrades {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int students;
System.out.println("How many students do you have?");
students = keyboard.nextInt();
while (students < 0) {
System.out.println("Invalid student amount");
students = keyboard.nextInt();
}
int[] kids = new int[students];
double[] scores = new double[students];
for (int index = 0; index < students; index++) {
System.out.println("Enter score for student #" + (index + 1) + ": ");
scores[index] = keyboard.nextDouble();
}
double high = scores[0];
for (int index = 1; index < scores.length; index++) {
if (scores[index] > high) ;
high = scores[index];
}
if (//place in array >= (highest-10)
System.out.println("Student (whatever there position is) has an A")
}
}
So if anyone can give me some insight into this issue I'll be grateful, also if you see anything wrong with the code please tell me. Any help would be great. Just to clarify my issue is with trying to find the letter grade. look at the if statement at the bottom.
If I were you, I would do that in the following way:
public class Student implements Comparable{
int index;
private double grade;
public Student(int i, double d) {
grade = d;
index = i;
}
public int getIndex() {
return index;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
#Override
public int compareTo(Object o) {
Student s = (Student)o;
if (s.getGrade() < grade){
return -1;
} else if (s.getGrade() == grade) {
return 0;
}
return 1;
}
}
And the driver for the application is:
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
int amount;
System.out.println("How many students do you have?");
amount = keyboard.nextInt();
while (amount < 0) {
System.out.println("Invalid student amount");
amount = keyboard.nextInt();
}
Student[] students = new Student[amount];
for (int index = 0; index < amount; index++) {
System.out.println("Enter score for student #" + (index + 1) + ": ");
students[index] = new Student(index, keyboard.nextDouble());
}
Arrays.sort(students);
for (int i = 0; i < amount / 10 + 1; i++) {
System.out.println("Student " + (students[i].getIndex() + 1) + " has an A");
}
}
Edit:
additional clarification for the OP asked in comments:
double highest = students[0].getGrade();
for (Student s : students) {
if (s.getGrade() > highest) {
highest = s.getGrade();
}
}

Changing input variables in a java loop

I have an assignment, and I need to use a loop to allow a user to enter ten different numbers in a programme which then adds up the variables.
I have found various pieces of code and stitched them together to create this:
import javax.swing.*;
import java.util.Scanner;
public class exercise6
{
public static void main (String []args)
{
//Input
String totalNum, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
Scanner in = new Scanner (System.in);
System.out.println("Please enter ten numbers:");
int[] inputs = new int[10];
for (int i = 0; i < inputs.length; ++i)
{
inputs[i] = in.next();
}
//Process
totalNum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
//Output
JOptionPane.showMessageDialog(null, "Total = " + totalNum);
}
}
It's not great, but it's the best I have so far. Please help?
You don't need the variables num1 to num10. You can simply sum up in the loop itself. Like:
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += = in.next(); // sum = sum + in.next();
}
Furthermore you assigned your variables as Strings, but you need int. In your case it would print something like 1111111111, if the input would always be a 1.
Take a look here how you would handle Integers properly.
You can achieve that in two ways, either inside the loop itself just add the number or if you need to keep track of them for later just add them to the array.
import javax.swing.*;
import java.util.Scanner;
public class exercise6
{
public static void main (String []args)
{
String total;
Scanner in = new Scanner (System.in);
int numOfInputValues = 10;
System.out.println("Please enter ten numbers:");
int[] inputs = new int[numOfInputValues];
for (int i = 0; i < numOfInputValues; ++i)
{
// Append to array only if you need to keep track of input
inputs[i] = in.next();
// Parses to integer
total += in.nextInt();
}
//Output
JOptionPane.showMessageDialog(null, "Total = " + totalNum);
}
}
First of all, your class should be in CamelCase. First letter is always in capital letter.
Second, you don't need an array to save those numbers.
Third you should make a global variable that you can change with ease. That is a good practice.
And you should always close stream objects like Scanner, because they leak memory.
import java.util.Scanner;
public class Exercise6 {
public static void main(String[] args) {
int numberQuantity = 10;
int totalNum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Please enter ten numbers:");
for (int i = 0; i <= numberQuantity; i++) {
totalNum += in.nextInt();
}
in.close();
System.out.println(totalNum);
}
}
So the simplest answer I found is:
import javax.swing.*;
import java.util.Scanner;
public class exercise6
{
public static void main (String []args)
{
//Input
int totalNum, num1;
totalNum = 0;
for (int numbers = 1 /*declare*/; numbers <= 10/*initialise*/; numbers ++/*increment*/)
{
num1 = Integer.parseInt(JOptionPane.showInputDialog("Input any number:"));
totalNum = totalNum + num1;
}
//Output
JOptionPane.showMessageDialog(null, "Total = " + totalNum);
Try this way I only re-edit your code:
import javax.swing.*;
public class InputNums {
public static void main(String[] args) {
int total = 0;
for (int i = 0, n = 0; i < 10;) {
boolean flag = false;
try {
n = Integer.parseInt(JOptionPane
.showInputDialog("Input any number:"));
} catch (NumberFormatException nfe) {
flag = true;
}
if (flag) {
flag = false;
JOptionPane.showMessageDialog(null,
"Invalid no Entered\nEnter Again...");
continue;
}
total += n;
i++;
}
JOptionPane.showMessageDialog(null, "Total = " + total);
}
}

Java scanner no error appears

I'm writing one of my first java codes and I can't get it to run. When I run the code it tells me there are errors but in the console at the bottom nothing appears for me to see what the error might be. Please help?
package operators;
import java.util.Scanner;
public class assignment2ifelse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int quantity;
int packages = 99;
quantity = input.nextInt();
double dis1 = .2; // dis2 = .33, dis3 = .42, dis4 = .29;
double price = packages * quantity;
double discount = 0;
double finalprice = price * discount;
if (quantity < 20 && quantity > 9) {
System.out.println(price);
discount = price * dis1;
System.out.println(discount);
System.out.println(finalprice);
}
}
}
Try this code :
package operators;
import java.util.Scanner;
public class assignment2ifelse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int quantity;
int packages = 99;
System.out.println("Enter a quantity : ");
quantity = input.nextInt();
double dis1 = .2; // dis2 = .33, dis3 = .42, dis4 = .29;
double price = packages * quantity;
double discount = 0;
double finalprice = price * discount;
if (quantity < 20 && quantity > 9) {
System.out.println(price);
discount = price * dis1;
System.out.println(discount);
System.out.println(finalprice);
} else {
System.out.println("Quantity doesn't match expectations.");
}
}
}
Actually, the program runs but it doesn't show you anything because it begins immediately by waiting for an input from the user:
quantity = input.nextInt();
Then, it shows you something only if your entry matches the condition of your if test.
Next time, try to create an interaction with the user by prompting them with messages using System.out.print().

How can I make a program which ask the user to enter number of limit odd number?

For example, if I enter 5, then the first 5 odd numbers will be shown, like 1,3,5,7,9.
import java.util.Scanner;
/**
*
* #author Hameed_Khan
*/
public class JavaApplication20 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int i;
System.out.println("Enter limit of ODD num");
i=obj.nextInt();
for(int n=0;n<10;n++){
if(n%2!=0){
int count=n;
while(count!=i)
System.out.print("\t"+n);
n++;
}
}
}
}
If I understand your question, I'd suggest a simple for loop.
int limit = (what ever user input you use);
int oddNums = 1;
for(int ii = 0; ii < limit; ii++ )
{
System.out.println(oddNums);
oddNums += 2;
}
Here you go.
import java.util.Scanner;
public class JavaApplication20 {
public static void main(String args[]) {
System.out.println("Enter an integer");
Scanner in = new Scanner(System.in);
int count = in.nextInt();
for(int i =1, j=1 ; i <= count ; j+=2,i++){
System.out.print(j);
if(i < count) System.out.print(",");
}
}
}
Hope you understand whats happening with the loop and the 2 variables.
Here i control the no. of iterations i.e. equal to the integer entered by the user and j is used to store the value of the odd number and is always incremented by 2 to get the next odd number.
Try its as:
public class JavaApplication20 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
{
Scanner obj=new Scanner(System.in);
int i,count=0;
System.out.println("Enter limit of ODD num");
i=obj.nextInt();
for(int n=0;;n++)
{
if(n%2!=0 && count!=i)
{
System.out.print("\t"+n);
count++;
}
}
}
}

Categories