Empty string in number formatting - java

This is my assignment:
Here are my questions:
How can I fix this error:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1012)
at java.lang.Double.parseDouble(Double.java:527)
at extracredit.Main.readData(Main.java:72)
at extracredit.Main.main(Main.java:27)
Are there any other problems that you can see with this program?
Here's my code so far:
import java.io.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
String fname = "data.txt"; //Read in the data file for use in the array
String pass= JOptionPane.showInputDialog("Please enter the " +
"password to continue:"); /*Have the user enter the password to
access the file. */
checkPass(pass); // Verify that the password is correct before continuing.
readData (fname); // Read data, print output and save output file.
}
private static void checkPass (String pass)
{
String password= "INF260";
int passCount= 0;
if (pass.equals(password)) {
System.out.println("The password is correct. Continuing...");
}
else {
do {
pass= JOptionPane.showInputDialog("Please re-enter the" +
"password:");
passCount++;
} while (!pass.equals(password) && passCount < 2);
if (!pass.equals(password)) {
System.out.println("You have tried to enter the " +
"password too many times. Exiting...");
System.exit(0);
}
else {
System.out.println("The password is correct. Continuing...");
}
}
}
public static void readData (String data) throws IOException{
FileReader inputData= new FileReader (data);
BufferedReader findNum= new BufferedReader (inputData);
String str= findNum.readLine ();
int count=-1;
int countNum= 0;
double total= 0;
double min= 0;
double max= 0;
double average= 0;
FileWriter writeFile = new FileWriter("sales.txt");
PrintWriter printFile = new PrintWriter(writeFile);
while (str != null)
{
double num= Double.parseDouble (str);
if (count == 0){
countNum++; // counter of Reciepts to use
}
str = findNum.readLine();
}
double [][] input = new double [countNum][10];
total= getCurrentTotal(input); /*This will get the total
from the method getCurrentTotal.*/
min= getCurrentMin(input); /*This will get the minimum value from
the method getCurrentMin.*/
max= getCurrentMax (input); /*This will get the maximum value from
the method getCurrentMax.*/
average= (total / countNum); //Calculate the average.
System.out.println("The List of Today's Sales:");
for (int row = 0; row < input.length; row++){
System.out.println ();
System.out.println("Customer " + row + "\t");
for (int column = 0; column < input[row].length; column++){
if (input [row].length < 10){
System.out.println(input[row][column] + "\t");
str = findNum.readLine();
}
else{
System.out.println ("There are too many receipts" +
" for one Customer.\n");
System.exit (0);
}
}
}
System.out.println ("There are " + countNum + "receipts in the list.");
/*This will print the total of receipts in the list.*/
System.out.println ("The total of today's sales is $" + total); /*
This will print the total of the sales for the day.*/
System.out.println ("The average of today's sales is $" + average); /*
This will print the average sale total.*/
System.out.println ("The highest receipt is $" + max); /* This will print
the highest sale.*/
System.out.println ("The lowest receipt is $" + min); /* This will print
the lowest sale.*/
Date date = new Date();
System.out.println ("\n The current time is:" + date.toString()); /* This
will print the current date and time */
}
public static double getCurrentTotal (double [][] input){
double totalAmount = 0;
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
totalAmount += input [row][column];
}
}
return totalAmount;
}
public static double getCurrentMin (double [][] input) {
double currentMin = input[0][0];
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
if (currentMin > input[row][column])
currentMin = input[row][column];
}
}
return currentMin;
}
public static double getCurrentMax (double [][] input){
double currentMax = input[0][0];
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
if (currentMax < input[row][column]){
currentMax = input[row][column];
}
}
}
return currentMax;
}
}

The best solution is:
study your course material
start with a subset of the problem like just reading the file.
test it
loop over:
continue to improve and enhance the program until it fulfills all the requirements.
test it
hand it in

// from your main method
String fname = "data.txt";
readData (fname);
// the method being called
public static void readData (String data[][]){
BufferedReader br = new BufferedReader(new FileReader(data));
We have an incompatibility here.
fname is a String
The method takes a String[] as a parameter.
the constructor newFileReader() takes a string, not 2d array.
All of these three should be the same data type.
How can I separate each "receipt" with zero (like shown in the image link above)?
You don't have to. You have to READ from that file with the zeros in it.
I would recommend you write a method something like this:
public double[] readOneReceipt(BufferedReader reader);
This method should
Read line by line until it encounters a 0 entry
for each entry it reads convert the value into a number (double?)
Store the number in a temporary structure.
When you encounter the "0", create a new array of the correct size and copy the read values into it.
How can I write the output into a separate file?
With a java.io.FileWriter
The hardest bit of this IMO is the fact that you are told to store the data in a 2d array, but you don't exactly what size to make the array until you've read the data.
Which means that you either use a temporary dynamic structure, or read the file twice - once to find out how many receipts there are so that you can make the array, and then again to actually read the receipt data.

Related

JAVA array pair for student grades [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
how do I pair the student array with the grade array? When I find the highest grade the corresponding student should also show, and same with the lowest graded student. I cant figure out how to make this program perform as such with two separate arrays.
import java.util.Scanner;
public class Asm7 {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("How many students do you have?: ");
int AMOUNT = 0;
AMOUNT = Scan.nextInt();
String[] STUDENT = new String [AMOUNT];
int COUNTER = 0;
int GRADE [] = new int [AMOUNT];
if (AMOUNT <= 0) {
System.out.println("Invalid student amount");
}
else {
for(int i = 0; i < AMOUNT; i++){
System.out.println("Enter student's first name: " + (i+1));
STUDENT[i] = Scan.next();
System.out.println("Enter student's grade in order added: ");
GRADE[i] = Scan.nextInt();
}
for(int i = 0; i < AMOUNT; i++){
System.out.println(STUDENT[i] + " received the final grade of " + GRADE[i]);}
System.out.println();
int [] Results = MinMax(GRADE);
System.out.println("The highest grade in the class was " + Results[1]);
System.out.println("The lowest grade in the class was "+ Results[0]);
}}
public static int[] MinMax(int[] value) {
int[] Result = new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int i : value) {
Result[0] = i < Result[0] ? i : Result[0];
Result[1] = i > Result[1] ? i : Result[1];
}
return Result;
}
}
Your while loop validation for number of students is a little late. You want to do this before you declare and initialize your arrays. However, the fact that the while loop was actually used in an attempt towards some form of validation is a really good sign. It's more than most new programmers tend to do. All input should be validated and provide a User the opportunity to supply a correct solution. This can only lead to a smoother, trouble free application and a much better experience for the User. Take a look at this while loop which is in your code:
while (amount < 0) {
System.out.println("Invalid student amount");
}
What is going to happen if the User supplies -1 (this is a valid integer value as is +1)? That's right...your application will end up in an infinite loop spitting out Invalid student amount to the Console Window. Your validation scheme should encompass the entire prompt and then the means to exit it should be more logically defined. With a while loop the best exit is done through its conditional statement, if the condition is false then exit the loop, for example:
Scanner scan = new Scanner(System.in);
// Number Of Students...
String inputString = "";
while (inputString.isEmpty()) {
System.out.print("How many students do you have?: --> ");
inputString = scan.nextLine().trim();
/* Is the supplied Number Of Students valid and within
range (1 to 50 inclusive)? */
if (!inputString.matches("\\d+") || Integer.valueOf(inputString) < 1
|| Integer.valueOf(inputString) > 50) {
// No...
System.err.println("Invalid entry (" + inputString + ") for Student "
+ "amount! Try again...");
inputString = ""; // Empty inputString so we loop again.
System.out.println();
}
}
// Valid amount provided.
int amount = Integer.valueOf(inputString);
String[] student = new String[amount];
int grade[] = new int[amount];
Right away you will notice some obvious changes here. The entire How many students do you have? prompt is contained within a while loop block. If the User does not supply a valid response then that User is asked to try again. The student and grade parallel arrays are declared and initialized only after a valid response for the number of students is provided.
You will also notice that the while loop condition doesn't rely on a integer value but instead it relies on actual string content (regardless of what it is) instead. If the variable is empty ("") then loop again. This is because the Scanner#nextLine() method is used to collect the Users input instead of the Scanner#nextInt() method. The prompt still expects an integer value to be supplied, just a string representation of an integer value and this is validated using the String#matches() method along with a small Regular Expression (regex).
I personally prefer to use the Scanner#nextLine() method for a number of reasons. I personally find it more flexible especially if you want to accept both Alpha and Numerical input from a single prompt. If the prompt above stated:
How many students do you have? (q to quit)
you would just need to add another if statement above the numerical validation code to see if 'q' or 'Q' was supplied, for example:
// If either q or Q is entered then quit application.
if (amountString.matches("[qQ]")) {
System.out.println("Bye-Bye");
System.exit(0);
}
Also, with a good expression passed to the matches() method, there is no need to trap exceptions in order to carry out validations, not that there is anything wrong with this, many people do it, I especially don't however when I have no need to do so.
Side Note: I'm going to state the obvious here and I'm sure you've heard it a hundred times before and you're sick of hearing it but I'm going to tell you again:
Your class methods should start with a lowercase letter (see Java Naming
Conventions).
I know you don't hear the compiler complaining but it does make it a
little more difficult (at times) to read the code. Everyone that reads
your code will appreciate you for it.
Because the student and grade arrays are parallel arrays you would want the minGrade() and maxGrade() methods to return a specific array index value to either the lowest or highest grade so that a referential relationship can be made toward the student that contains that specific grade determined. So, this would be far more useful:
public static int minGrade(int[] arr, int size) {
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
public static int maxGrade(int[] arr, int size) {
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
With everything in play your code might look like this:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Number Of Students...
String amountString = "";
while (amountString.isEmpty()) {
System.out.print("How many students do you have?: --> ");
amountString = scan.nextLine().trim();
// Is the supplied Number Of Students valid and within
// range (1 to 50 inclusive)?
if (!amountString.matches("\\d+") || Integer.valueOf(amountString) < 1
|| Integer.valueOf(amountString) > 50) {
// No...
System.err.println("Invalid entry (" + amountString + ") for Student "
+ "amount! Try again...");
amountString = ""; // Empty inputString so we loop again.
System.out.println();
}
}
// Valid amount provided.
int amount = Integer.valueOf(amountString);
// Declare and initialize parallel arrays
String[] student = new String[amount];
int grade[] = new int[amount];
// Student Names and Grade...
for (int i = 0; i < amount; i++) {
// Student Name...
String name = "";
while (name.isEmpty()) {
System.out.print("Enter student #" + (i + 1) + " name: --> ");
name = scan.nextLine().trim();
/* Is the name valid (contains upper or lower case letters from
A-Z and a single whitespaces separating first and last name?
Whitespace and last name is optional. */
if (!name.matches("(?i)([a-z]+)(\\s{1})?([a-z]+)?")) {
// No..
System.err.println("Invalid Student #" + (i + 1) + " name ("
+ name + ")! Try Again...");
System.out.println();
name = ""; // Empty name so we loop again.
}
}
// Valid Student name provided...
student[i] = name;
// Student Grade...
String gradeString = "";
while (gradeString.isEmpty()) {
System.out.print("Enter student #" + (i + 1) + " grade: --> ");
gradeString = scan.nextLine().trim();
// Is the supplied grade valid and within range (0 to 100 inclusive)?
if (!gradeString.matches("\\d+")
|| Integer.valueOf(gradeString) < 0
|| Integer.valueOf(gradeString) > 100) {
// No...
System.err.println("Invalid entry (" + gradeString + ") for "
+ "Student #" + (i + 1) + " grade! Try again...");
gradeString = "";
System.out.println();
}
}
// Valid Student grade provided...
grade[i] = Integer.valueOf(gradeString);
}
// Display everyone's grade
System.out.println();
for (int i = 0; i < amount; i++) {
System.out.println(student[i] + " received the final grade of " + grade[i]);
}
System.out.println();
//Display who is highest and lowest...
int index = maxGrade(grade, amount);
System.out.println("The highest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
index = minGrade(grade, amount);
System.out.println("The lowest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
}
public static int minGrade(int[] arr, int size) {
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
public static int maxGrade(int[] arr, int size) {
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
If the data are not sorted, it would be better to find both min and max grades in the same loop, after printing the students and their grades.
Then no loop is needed to print min and max grades:
for (int i = 0; i < amount; i++) {
System.out.println(student[i] + " received the final grade of " + grade[i]);
}
int min = grade[0];
int max = grade[0];
for (int i = 1; i < amount; i++) {
if (grade[i] < min) {
min = grade[i];
} else if (grade[i] > max) {
max = grade[i];
}
}
System.out.println("The highest grade in the class was " + max);
System.out.println("The lowest grade in the class was " + min);
If the index of min/max is sought, it would be possible to print the name of the students who get the min and max grades.
public static void main(String[] args) {
int[] grades = new int[]{50, 51, 52, 50, 60, 22, 53, 70, 60, 94, 56, 41};
int[] result = getMinMax(grades);
System.out.println("Min: " + result[0] + ", Max: " + result[1]);
}
public static int[] getMinMax(int[] values) {
int[] result = new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int i : values) {
result[0] = i < result[0] ? i : result[0];
result[1] = i > result[1] ? i : result[1];
}
return result;
}
You'll need to handle the case of int[] values being null or empty. You can decide that (Throw an exception, return null... or whatever)

How can I add the values that I got from multiplying two arrays?

So I am creating an invoice program and I got stuck at the part when I have to get the total I got from multiplying two arrays. I am able to multiply them and I get the values but unfortunately more than one value if I enter more than one item. I want to be able to add the values I get to get a total.
To give you an idea, here's my code:
public static void main(String []args){
Scanner input = new Scanner(System.in);
String sentinel = "End";
String description[] = new String[100];
int quantity[] = new int[100];
double price [] = new double[100];
int i = 0;
// do loop to get input from user until user enters sentinel to terminate data entry
do
{
System.out.println("Enter the Product Description or " + sentinel + " to stop");
description[i] = input.next();
// If user input is not the sentinal then get the quantity and price and increase the index
if (!(description[i].equalsIgnoreCase(sentinel))) {
System.out.println("Enter the Quantity");
quantity[i] = input.nextInt();
System.out.println("Enter the Product Price ");
price[i] = input.nextDouble();
}
i++;
} while (!(description[i-1].equalsIgnoreCase(sentinel)));
System.out.println("Item Description: ");
System.out.println("-------------------");
for(int a = 0; a <description.length; a++){
if(description[a]!=null){
System.out.println(description[a]);
}
}
System.out.println("-------------------\n");
System.out.println("Quantity:");
System.out.println("-------------------");
for(int b = 0; b <quantity.length; b++){
if(quantity[b]!=0){
System.out.println(quantity[b]);
}
}
System.out.println("-------------------\n");
System.out.println("Price:");
System.out.println("-------------------");
for(int c = 0; c <price.length; c++){
if(price[c]!=0){
System.out.println("$"+price[c]);
}
}
System.out.println("-------------------");
//THIS IS WHERE I MULTIPLY THE PRICE AND THE QUANTIY TOGETHER TO GET THE TOTAL
for (int j = 0; j < quantity.length; j++)
{
//double total;
double total;
total = quantity[j] * price[j];
if(total != 0){
System.out.println("Total: " + total);
}
}
}
}
In your last for loop, you're only multiplying the quantity and the price of the item and putting it as the value of total instead of adding it to total. Also every time it loops it creates a new total.To make it better, declare total out of the loop and move your if statement out
double total = 0.0;
for (int j = 0; j < quantity.length; j++){
total += quantity[j] * price[j];
}
if(total != 0){
System.out.println("Total: " + total);
}

Incorrect output in Java program

This is the description of the problem and here is the code that I got:
PP 10.1 Design and implement a program that reads a series of 10 inte- gers from
the user and prints their average. Read each input value as a string, and then
attempt to convert it to an integer using the Integer.parseInt method. If this process
throws a NumberFormatException (meaning that the input is not a valid number),
print an appropriate error message and prompt for the number again. Continue
reading values until 10 valid integers have been entered.
This is the code I have so far:
import java.util.Scanner;
public class average
{
private int number_values;
private int[] int_values;
private double average;
public average(int number_values)
{
this.number_values = number_values;
}
public void values()
{
String value_string = null;
int int_value = 0,a;
Scanner number = null;
a = 0;
int_values = new int [number_values];
while (a < number_values)
{
try{
number = new Scanner(System.in);
System.out.print("Please enter a value:");
value_string = number.nextLine();
int_value = Integer.parseInt(value_string);
int_values[a++] = int_value;
}
catch (NumberFormatException ex)
{
System.out.print("This is an invalid input. Please renter another number:");
continue;
}
}
}
public void printValues()
{
System.out.println("The values are:");
for (int a = 0; a < number_values; a++)
{
System.out.println("Number - " + (a + 1) + " = " + int_values);
}
}
public double get_average()
{
int sum = 0;
for(int a = 0; a < number_values; a++)
{
sum += int_values[a];
}
average = (double) sum / number_values;
return (average);
}
public static void main(String[] args)
{
average a = new average(10);
a.values();
a.printValues();
System.out.println("Average = " + a.get_average());
}
}
When I enter an incorrect character it says "This is an invalid input. Please renter another number:Please enter a value:"
And when I displays the average it says Number - 1 = [I#330bedb4" for all of the values.
So the println string when I enter a incorrect input is messed up and the values are messed up. What am I missing?
Try intValues[i], if you want to output a specific item of your array. Otherwise you get the toString representation of the array object itself.
And please next time post your code instread of screenshots ;)
You're not printing the elements of the array. You're printing the array string representation directly. The error is done in your printValues() method.
To solve this, you must call an element of the array. In this case, you want to call all elements, so you must use a loop. Here:
public void printValues()
{
System.out.println("The values are:");
for (int a = 0; a < number_values; a++)
{
System.out.println("Number - " + (a + 1) + " = " + int_values[i]);
}
}

How do you incorperate an AVERAGE and LETTERGRADE in a java program which has 2D Array

I have written a program with minimal errors but do not know where I should place the Average or Letter grade function:
package org.education.tutorial;
import java.util.Scanner;
public class GradingScale
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the number of students attending your current session :");
int numberOfStudents = keyboard.nextInt();
System.out.println();
System.out.print("Also, please enter the amount of exams taken during the duration of the course : ");
int examScores = keyboard.nextInt();
AssignValueToArray(numberOfStudents, examScores);
keyboard.close();
}
public static void AssignValueToArray(int amountOfStudents, int amountOfExams)
{
int[][] overallScore = new int[amountOfStudents][amountOfExams];
Scanner keyboardArray = new Scanner(System.in);
int numberValue = 1;
for (int index = 0; index < amountOfStudents; index++)
{
System.out.println ("\n" + "Please submit Student #" + numberValue + " 's score :" );
for(int indexOfHomeWork=0; indexOfHomeWork < amountOfExams; indexOfHomeWork++)
{
overallScore[index][indexOfHomeWork] = keyboardArray.nextInt();
}
numberValue++;
}
DisplayvalueInArray(overallScore);
keyboardArray.close();
}
public static void DisplayvalueInArray(int[][] overallScoreArray)
{
System.out.println ("The students' scores are posted below : " + "\n");
int studentCount = 1;
for (int index = 0; index < overallScoreArray.length; index++)
{
System.out.print("Grades for student " + studentCount +": ");
for (int indexOfHomeWork = 0; indexOfHomeWork < overallScoreArray[index].length;
indexOfHomeWork++)
{
System.out.print(overallScoreArray[index][indexOfHomeWork]+"\t");
}
System.out.println();
studentCount++;
}
}
I would highly recommend learning how to use variable scope and possibly objects before doing something such as this.
You never declare any variables in the class's scope.
Most of your functions don't affect anything outside their own scope, and therefore shouldn't be functions. Instead use comments to encapsulate.
Many of your variables are named poorly, instead of keyboardArray, which is not an array at all, do something like kb, or keyboardScanner
For computing average and lettergrades, think 'what am i averaging' and use that as your parameter, with the average as your return type, so something like static int average(int[] scores)

Find and Print high number array method Java

What I’m attempting to do is search “gradePsd” array find the highest grade and if there are two grades that are the same value print the name (s) of the students to console.
The problem I’m having is that this method is taking the first index value of the array and printing it because it IS the high value at the first pass and if the second value is larger than the first then it will also print and so on.
So my question is how can I get it to just print the student (s) with the high grade.
public static void hiMarkMethod(String[] NamePsd, int[] gradePsd)
{
String nameRtn = "";
int num = gradePsd[0];
System.out.println ("\n\nThe Student(s) with Hightest Mark(s) are:");
for (int i = 0; i < gradePsd.length; i++)
{
if (gradePsd[i] >= num)
{
num = gradePsd[i];
nameRtn = NamePsd[i];
}
System.out.print(nameRtn + ", ");
}
}
first find the highest number
then print the students with that number
public static void hiMarkMethod(String[] NamePsd, int[] gradePsd)
{
String nameRtn = "";
int num = gradePsd[0];
System.out.println ("\n\nThe Student(s) with Hightest Mark(s) are:");
//find the highest number
for (int i = 0; i < gradePsd.length; i++){
if (gradePsd[i] >= num){
num = gradePsd[i];
}
//print students with that number
for (int j = 0; j < NamePsd.length; j++){
if (gradePsd[j] == num)
{
nameRtn = NamePsd[j];
System.out.print(nameRtn + ", ");
}
}
one of possible 1000 solutions.
Initialize num with -1 and take the System.out out of the for loop. But you can only determine one student with your code. You need nameRtn to be a Collection if you want to store more than one name.
Something like this:
public static void hiMarkMethod(String[] NamePsd, int[] gradePsd) {
Collection<String> namesRtn = new ArrayList<String>();
int num = -1;
for (int i = 0; i < gradePsd.length; i++) {
if (gradePsd[i] > num) {
num = gradePsd[i];
namesRtn.clear(); // clear name list as we have a new highest grade
namesRtn.add(NamePsd[i]); // store name in list
} else if (gradePsd[i] == num) {
namesRtn.add(NamePsd[i]); // if a second student has the same grade store it to the list
}
}
System.out.println ("\n\nThe Student(s) with Hightest Mark(s) are: " + namesRtn);
}

Categories

Resources