I keep getting this error message I have checked my file names, and my public class is the same as my .java file. Besides checking that I have no idea where to go.
Main.java:3: error: class ClassGenderPercentages is public, should be declared in a file named ClassGenderPercentages.java
public class ClassGenderPercentages
^
Here is my code
import java.util.Scanner;
public class ClassGenderPercentages
{
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
int maleStudents, femaleStudents;
int totalStudents;
double maleStudentPercentage, femaleStudentPercentage;
maleStudents = keyboard.nextInt();
System.out.println("Enter number of male registered:" + maleStudents);
femaleStudents = keyboard.nextInt();
System.out.println("Enter number of female registered:" + femaleStudents);
totalStudents = (int) (maleStudents + femaleStudents);
maleStudentPercentage = ((100 * maleStudents) / totalStudents);
femaleStudentPercentage = ((100 * femaleStudents) / totalStudents);
System.out.println("The percentage of males registered is: " + maleStudentPercentage + "%");
System.out.println("The percentage of females registed is: " + femaleStudentPercentage + "%");
}
}
Your files is named Main.java, your class ClassGenderPercentages.
You can rename your file to ClassGenderPercentages.java
(that is already said in the error you received from the compiler)
Related
I'm getting two errors on this code. One on the last line on the System.out.println and one when i'm calling the method. How would I make this code work with this file? Here is my code:
package practicefile;
/**
*
* #author jahkeyshagodwin
*/
import java.io.*;
import java.util.*;
public class Practicefile {
//import java.util.*;
/**
* #param args the command line arguments
* #throws java.io.FileNotFoundException
*/
public static void main(String[] args)
throws FileNotFoundException {
Scanner input = new Scanner(new File("hotel.txt"));
PrintStream(Scanner (input));
}
public static void PrintStream(Scanner input) {
while (input.hasNext()) {
String name = input.next();
double sum = 0.0;
while (input.hasNextDouble()) {
sum += input.nextDouble();
}
} System.out.println("Total hours worked by" + name + " = " + sum);
}
}
The stack trace reads:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: practicefile.Practicefile.Scanner
at practicefile.Practicefile.main(Practicefile.java:23)
/Users/jahkeyshagodwin/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53:
Java returned: 1
BUILD FAILED (total time: 1 second)
One problem you have is with the line
PrintStream(Scanner (input));
The correct syntax for what you are trying to do is:
PrintStream(input);
Second is that in your loop you declare the variables name and sum. However because you declared them inside the loop, they do not exist outside the loop. If you change it to:
public static void PrintStream(Scanner input) {
String name = "";
double sum = 0.0;
while (input.hasNext()) {
name = input.next();
while (input.hasNextDouble()) {
sum += input.nextDouble();
}
} System.out.println("Total hours worked by" + name + " = " + sum);
}
It should work.
Essentially, I need to use the value from the getOriginalPrice method in a module.
When I simply put item.getOriginalPrice * .1, my code still compiles.
Not sure whats going on with this one.
Here is my code (not pasting the original class code unless needed).
import java.util.Scanner;
class InventoryApp {
public static void main(String[] args) {
String invNum = " ";
double ogPrice = 1;
int finishItems;
Inventory saleItem;
saleItem = new Inventory();
Scanner keyb = new Scanner(System.in);
System.out.println("Input the item numbers and original price for each item");
System.out.println("and I will show you the discounted price of each item");
System.out.println("for each day of the sale this week.");
System.out.println("Enter stop at the first prompt to end your list.");
System.out.println("Enter the item number of the item (Ex. 2b)");
invNum = keyb.next();
saleItem.setItemNumber(invNum);
while (!invNum.equals("stop")) {
System.out.println("Enter the item's original price (Ex 23.50");
ogPrice = keyb.nextDouble();
saleItem.setOriginalPrice(ogPrice);
printSaleData(saleItem);
System.out.println("Enter the item's item number");
invNum = keyb.next();
saleItem.setItemNumber(invNum);
}//end While
}//end main
public static void printSaleData(Inventory item) {
System.out.println("Item Number: " + item.getItemNumber());
for(int x = 1; x < 7; x = x + 1) {
item.getOriginalPrice() = item.getOriginalPrice -
(item.getOriginalPrice * .1);
System.out.println("Day: " + x + "\t" + "Price: $" + item.getOriginalPrice());
}//end for
}
}//end Class
Your printSaleDate method is not valid java. From your posted info, it's not clear what's your expected output.
But strictly speaking this is your posted method once fixed:
for (int x=1; x<7; x++) {
double discountedPrice = item.getOriginalPrice() * 0.1;
System.out.println("Day: " + x + "\tPrice: $" + discountedPrice);
}
I'm trying to understand the different parts of the code but I need to ask for individual help at this point. So here's my issue: I'm building a simple grade average program for my first java programming class. I want to save 4 grade inputs, then display an average. Eventually I am going to display letter grades based on that average. I think this error is saying I am not initializing finalGrade.
But I'm lost. An explanation of what is happening would be great so I can actually learn this.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class GradeAverage{
public static Double gradeQ1; //gradeQ are grades for the respective quarters
public static Double gradeQ2;
public static Double gradeQ3;
public static Double gradeQ4;
public static String studentName;
public static Double finalGrade = ((gradeQ1 + gradeQ2 + gradeQ3 + gradeQ4) / 4);
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
studentName = JOptionPane.showInputDialog(null, "Please enter your first and last name.");
JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", let's get started!");
gradeQ1 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the first quarter?")); // gets grade and saves it as a double gradeQ1
JOptionPane.showMessageDialog(null, "You entered " + gradeQ1);
//double gradeQ1 = input.nextDouble();
gradeQ2 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the second quarter?"));
JOptionPane.showMessageDialog(null, "You entered " + gradeQ2);
gradeQ3 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the third quarter?"));
JOptionPane.showMessageDialog(null, "You entered " + gradeQ3);
gradeQ4 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the fourth quarter?"));
JOptionPane.showMessageDialog(null, "You entered " + gradeQ4);
JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", Your average was " + finalGrade);
}
}
JGRASP error:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at GradeAverage.<clinit>(GradeAverage.java:15)
Your program fail on:
public static Double finalGrade = ((gradeQ1 + gradeQ2 + gradeQ3 + gradeQ4) / 4);
because grade* are object and they haven't got initialization. The program could work only in case you use double instead Double. The different is that the first isn't an object and its default value is 0.0 , while Double is an object with default value null. This create the nullPointerException.
Second your finalGrade will be 0, you must before read the value and then set the finalGrade's value.
...
JOptionPane.showMessageDialog(null, "You entered " + gradeQ4);
finalGrade = ((gradeQ1 + gradeQ2 + gradeQ3 + gradeQ4) / 4);
JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", Your average was " + finalGrade);
I'm really new to java and i'm taking an introductory class to computer science. I need to know how to Prompt the user to user for two values, declare and define 2 variables to store the integers, and then be able to read the values in, and finally print the values out. But im pretty lost and i dont even know how to start i spent a whole day trying.. I really need some help/guidance. I need to do that for integers, decimal numbers and strings. Can someone help me?
this is what ive tried
import java.util.Scanner;
class VariableExample
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer value");
int a = scan.nextInt();
int b = scan.nextInt();
System.out.println("Please enter an integer value");
double c = scan.nextDouble();
double d = scan.nextDouble();
System.out.println("Please enter an integer value");
string e = scan.next();
string f = scan.next();
System.out.println("Your integer is: " + intValue + ", your real number is: "
+ decimalValue + " and your string is: " + textValue);
}
i told you... im really new
You forgot to declare entry point i.e. main() method, String S should be capital and in System.out.println() you used wrong variables:
class VariableExample {
public static void main(String... args) { // Entry point..
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer value");
int a = scan.nextInt();
int b = scan.nextInt();
System.out.println("Please enter an integer value");
double c = scan.nextDouble();
double d = scan.nextDouble();
System.out.println("Please enter an integer value");
String e = scan.next(); // String S should be capital..
String f = scan.next();
System.out.println("Your integer is: " + a + " " + b + ", your real number is: " + c + " " + d
+ " and your string is: " + e + " " + f); // here you used wrong variables
}
}
If still your problem not clear then let me know where you actually stuck.
There are a couple of issues.
(1) You need to declare an "entry" point for your program. In Java, you must create a method with the exact signature:
public static void main(String args) {
// Your code here
}
(2) The "string" type in Java is capitalized.
(3) You are referencing variables that have been neither declared nor defined:
System.out.println("Your integer is: " + intValue + ", your real number is: "
+ decimalValue + " and your string is: " + textValue);
In this case, you've never told Java what the value of intValue, etc is. It seems like you want to use the variables you have declared and defined like:
System.out.println("Your integer is: " + a + ", your real number is: "
+ c + " and your string is: " + e);
(4) It looks like you're reading in two sets of variables for each prompt. Based on your prompt "Please enter an...", you really are expecting one input.
Altogether, I think your code should look like this:
class VariableExample {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer value: ");
int a = scan.nextInt();
System.out.println("Please enter a double value: ");
double c = scan.nextDouble();
System.out.println("Please enter a string: ");
String e = scan.next();
System.out.println("Your integer is: " + a + ", your real number is: "
+ c + " and your string is: " + e);
}
}
I'm new to java programming and currently making this raffle program. Here is the sample output of the program.
Welcome to raffle 2013!
The Prize is <10 million - 100 million> //this is random. I already made.
Ticket number: <Generating unique 10 digits numbers>
//these are the required information for the user.
Name:
Address:
Contact:
Birthday:
//The final output
The winner of <PRIZE> is <NAME>, Ticket number.
I already have written some source code and I think I'm almost there. Unfortunately, I encountered a problem with the ticket number. The ticket number must generate 5 times with 10 digits number. The output must show the winner name together with his/her ticket number but the ticket number wasn't show and state that it is null. Here are the syntax I already made.
public class raffle2013 {
//Title: Raffle 2013
public static final int SIZE = 5;
public static final int SIZE1 = 5;
private static short x;
private static String randomNumber;
public static void main(String[] args) {
String[] names;
names = new String[SIZE];
String[] winner;
winner = new String[SIZE1];
System.out.println("Welcome To Raffle 2013");
long Low = 10000000;
long High = 100000000;
long randomPrize = (long) (Math.random() * High - Low) + Low;
System.out.println("The Prize is " + " " + randomPrize);
for (int a = 0; a < winner.length; a++) {
long randomNumber = (long) (Math.random() * 9000000000L);
System.out.println("Ticket number: " + randomNumber);
Scanner scan = new Scanner(System.in);
System.out.print("Name " + ":");
winner[a] = scan.nextLine();
Scanner scan2 = new Scanner(System.in);
System.out.print("Address" + ":");
names[x] = scan.nextLine();
Scanner scan3 = new Scanner(System.in);
System.out.print("Contact" + ":");
names[x] = scan.nextLine();
Scanner scan4 = new Scanner(System.in);
System.out.print("Birthday" + ":");
names[x] = scan.nextLine();
System.out.println(" ");
}
Random random = new Random();
int w = random.nextInt(SIZE1);
System.out.println("The winner of" + " " + randomPrize + " " + "Million Peso(s)" + "is" + " " + winner[w] + "," + " " + "Ticket Number:" + " " + randomNumber);
}
}
Note: the program can generate 10 digits unique numbers for the ticket number and generate it 5 times together with the names, however I have a problem of choosing the ticket number winner.
Here is the output in java netbeans:
run:
Welcome To Raffle 2013
The Prize is 38375493
Ticket number: 1991318978
Name :a
Address:a
Contact:a
Birthday:a
Ticket number: 194313423
Name :b
Address:b
Contact:b
Birthday:b
Ticket number: 6017170047
Name :c
Address:c
Contact:c
Birthday:c
Ticket number: 274411236
Name :d
Address:d
Contact:d
Birthday:d
Ticket number: 6183250376
Name :e
Address:e
Contact:e
Birthday:e
The winner of 38375493 Million Peso(s)is a, Ticket Number: null<--- bug
BUILD SUCCESSFUL (total time: 18 seconds)
the last output must show the winner name together with his/her ticket number. I hope you could help me. Please :)
Actually you have created randomNumber of String type outside the loop , while inside you have created randomNumber of type long...this long type is only accessible within the loop and the randomNumber you are calling is the one which is string type and it is null because you havent assigned any value to it..
What you have to do is to have an array of the randomNumber and save the ticket numbers as well and in the last as you are getting the winner[w] the same way you get randomNumber[w]
Looks like you're running into an issue with scope. You may also be slightly confused about data types, but let's see...
randomNumber is assigned as a long inside of your loop, but it is declared as a static String inside your class. The declaration inside of the loop is not applicable when coming to a wider scope (that is, inside of your main method), so you won't get any pertinent values from randomNumber.
That's weird in and of itself - the different data types don't make sense to me. You also don't need the static declaration of randomNumber either - it's only ever used in main, so you could just declare it there.
To get around that, you have to wrap the output from your random input instead, and drop the declaration:
randomNumber = Long.toString(Double.valueOf(Math.random() * 9000000000L).longValue());
Also, as a code smell, you've got too many Scanner instances; you should only really need one. I leave that as an exercise to the reader to clean up and refactor.
Similar to storing the winner, the ticket number has to be stored as well, and recalled when the output is printed
import java.util.Random;
import java.util.Scanner;
public class raffle2013 {
// Title: Raffle 2013
public static final int SIZE = 5;
public static final int SIZE1 = 5;
private static short x;
private static String randomNumber;
public static void main(String[] args) {
String[] names;
names = new String[SIZE];
String[] winner;
winner = new String[SIZE1];
long[] ticketNumber = new long[SIZE1];
System.out.println("Welcome To Raffle 2013");
long Low = 10000000;
long High = 100000000;
long randomPrize = (long) (Math.random() * High - Low) + Low;
System.out.println("The Prize is " + " " + randomPrize);
for (int a = 0; a < winner.length; a++) {
long randomNumber = (long) (Math.random() * 9000000000L);
System.out.println("Ticket number: " + randomNumber);
Scanner scan = new Scanner(System.in);
System.out.print("Name " + ":");
winner[a] = scan.nextLine();
ticketNumber[a] = randomNumber;
Scanner scan2 = new Scanner(System.in);
System.out.print("Address" + ":");
names[x] = scan.nextLine();
Scanner scan3 = new Scanner(System.in);
System.out.print("Contact" + ":");
names[x] = scan.nextLine();
Scanner scan4 = new Scanner(System.in);
System.out.print("Birthday" + ":");
names[x] = scan.nextLine();
System.out.println(" ");
}
Random random = new Random();
int w = random.nextInt(SIZE1);
System.out.println("The winner of" + " " + randomPrize + " "
+ "Million Peso(s)" + "is" + " " + winner[w] + "," + " "
+ "Ticket Number:" + " " + Long.toString(ticketNumber[w]));
}
}