I am new to java, and going to bite the bullet by asking what is I am sure, a dumb question. I created some methods, and simply wanted to call them in main. I am getting an error for the while loop in the main method. The compiler is saying " Exception in thread main java.lang.StringIndexOutOfBoundsException: String index out of range:0 at java.lang.String.charAt(String.java:686) at Project3.main(Project3.java:61)
Any help would be greatly appreciated. Thanks. Full Code is below:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Project3
{
public static void main(String[] args)
{
int iScore1; //first variable input by user to calc average
int iScore2; //second variable input by user to calc average
int iScore3; //third variable input by user to calc average
double dAverage; //returned average from the three test scores
char cLetterGrade; //letter grade associated with the average
double dGPA; //the GPA associated with the letter grade
char cIterate = 'Y'; // loop check
String strAgain; //string user inputs after being asked to run again
System.out.print(createWelcomeMessage());
//pause in program
pressAnyKey();
while (cIterate == 'Y')
{
//prompt user for test scores
System.out.print("\n\nPlease enter the first test score: ");
Scanner keys = new Scanner(System.in);
iScore1 = keys.nextInt();
System.out.print("\nPlease enter the second test score: ");
iScore2 = keys.nextInt();
System.out.print("\nPlease enter the third test score: ");
iScore3 = keys.nextInt();
//calculate average from the three test scores
dAverage = calcAverage(iScore1, iScore2,iScore3);
System.out.print("\nThe average of the three scores is: " + dAverage);
//pause in program
pressAnyKey();
//get letter grade associated with the average
cLetterGrade = getLetterGrade(dAverage);
System.out.print("\nThe letter grade associated with the average is " + cLetterGrade);
//pause in program
pressAnyKey();
//get the GPA associated with the letter grade
dPGA = calcGPA(cLetterGrade);
System.out.print("\nThe GPA associated with the GPA is "+ dGPA);
//pause in program
pressAnyKey();
System.out.print("\nDo you want to run again?(Y or N):_\b");
strAgain = keys.nextLine;
strAgain = strAgain.toUpperCase();
cIterate = strAgain.charAt(0);
}//end while
//display ending message to user
System.out.print(createEndingMessage());
}//end main method
}//end class Project3
public static String createWelcomeMessage()
{
String strWelcome;
strWelcome = "Why hello there!\n";
return strWelcome;
}//end createWelcomeMessage()
public static String createEndingMessage()
{
String strSalutation;
strSalutation = "See you later!\n";
return strSalutation;
}//end createEndingMessage()
public static void pressAnyKey()
{
JOptionPane.showMessageDialog(null, "Press any key to continue: ");
}//end pressAnyKey()
public static int getTestSCore()
{
int iScore;
System.out.print("Enter a test score: ");
Scanner keys = new Scanner(System.in);
iScore = keys.nextInt();
return iScore;
}//end getTestSCore()
public static int calcAverage(int iNum1, int iNum2, int iNum3)
{
double dAverage;
dAverage = ((double)iNum1 + (double)iNum2 + (double)iNum3) / (double)3.0;
return dAverage;
}//end calcAverage(int iNum1, int iNum2, int iNum3)
public static char getLetterGrade(double dGrade)
{
char cLetter;
if (dGrade <60)
{
cLetter = 'F';
}
else if (dGrade >=60 && dGrade <70)
{
cLetter = 'D';
}
else if (dGrade >=70 && dGrade <80)
{
cLetter = 'C';
}
else if (dGrade >=80 && dGrade <90)
{
cLetter = 'B';
}
else if (dGrade >=90)
{
cLetter = 'A';
}
return cLetter;
}//end getLetterGrade(double dGrade)
public static double calcGPA(char cLetterGrade)
{
double dGPA;
if (cLetterGrade == 'A')
{
dGPA = 4.0;
}
else if (cLetterGrade == 'B')
{
dGPA = 3.0;
}
else if (cLetterGrade == 'C')
{
dGPA = 2.0;
}
else if (cLetterGrade == 'D')
{
dGPA = 1.0;
}
else
{
dGPA = 0.0;
}
return dGPA;
}//end calcGPA(char cLetterGrade)
You're reading three ints using scanner.nextInt(). Since nextInt does not consume any whitespace or newline after the read token, that means that if the user enters a number and presses enter, there's still a linebreak in the stream.
So when you call nextLine later it just reads that linebreak and returns the empty string.
Since calling charAt on an empty string will cause an out of bounds error, you get the error you get.
To fix this, either use next instead of nextLine, which will read the next word (consuming any whitespace before it), instead of the next line, or call nextLine twice. Once to consume the linebreak and once to read the actual line.
You should still check whether the user enters an empty line though.
The problem is caused by this line:
cIterate = strAgain.charAt(0);
The string apparently does not have a character at index 0, in other words, it is empty.
You may want to check the user input and ask again if none was supplied.
If you move line 67 or so, it's the line that ends the class. Move it to the end, and that brings it down to three errors. One of those three errors is a misspelling, the one about keys.nextLine needs () --> keys.nextLine() and the last error is that the method header returns an int, not a double. Doing this does generate another error, but if you set cLetter to a space in single quotes, ' ' then the code compiles.
Related
I'm creating a simple average calculator using user input on Eclipse, and I am getting this error:
" java.util.NoSuchElementException: No line found " at
String input = sc.nextLine();
Also I think there will be follow up errors because I am not sure if I can have two variables string and float for user input.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
String input = sc.nextLine();
float num = sc.nextFloat();
float sum = 0;
int counter = 0;
float average = 0;
while(input != "done"){
sum += num;
counter ++;
average = sum / counter;
}
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}
Thanks a lot:)
First, the precision of float is just so bad that you're doing yourself a disservice using it. You should always use double unless you have a very specific need to use float.
When comparing strings, use equals(). See "How do I compare strings in Java?" for more information.
Since it seems you want the user to keep entering numbers, you need to call nextDouble() as part of the loop. And since you seem to want the user to enter text to end input, you need to call hasNextDouble() to prevent getting an InputMismatchException. Use next() to get a single word, so you can check if it is the word "done".
Like this:
Scanner sc = new Scanner(System.in);
double sum = 0;
int counter = 0;
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
for (;;) { // forever loop. You could also use 'while (true)' if you prefer
if (sc.hasNextDouble()) {
double num = sc.nextDouble();
sum += num;
counter++;
} else {
String word = sc.next();
if (word.equalsIgnoreCase("done"))
break; // exit the forever loop
sc.nextLine(); // discard rest of line
System.out.println("\"" + word + "\" is not a valid number. Enter valid number or enter \"done\" (without the quotes)");
}
}
double average = sum / counter;
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
Sample Output
Enter the numbers you would like to average. Enter "done"
1
2 O done
"O" is not a valid number. Enter valid number or enter "done" (without the quotes)
0 done
The average of the 3 numbers you entered is 1.0
So there are a few issues with this code:
Since you want to have the user either enter a number or the command "done", you have to use sc.nextLine();. This is because if you use both sc.nextLine(); and sc.nextFloat();, the program will first try to receive a string and then a number.
You aren't updating the input variable in the loop, it will only ask for one input and stop.
And string comparing is weird in Java (you can't use != or ==). You need to use stra.equals(strb).
To implement the changes:
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
float sum = 0;
int counter = 0;
String input = sc.nextLine();
while (true) {
try {
//Try interpreting input as float
sum += Float.parseFloat(input);
counter++;
} catch (NumberFormatException e) {
//Turns out we were wrong!
//Check if the user entered done, if not notify them of the error!
if (input.equalsIgnoreCase("done"))
break;
else
System.out.println("'" + input + "'" + " is not a valid number!");
}
// read another line
input = sc.nextLine();
}
// Avoid a divide by zero error!
if (counter == 0) {
System.out.println("You entered no numbers!");
return;
}
// As #Andreas said in the comments, even though counter is an int, since sum is a float, Java will implicitly cast coutner to an float.
float average = sum / counter;
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\" at end : ");
String input = scanner.nextLine();
float num = 0;
float sum = 0;
int counter = 0;
float average = 0;
while(!"done".equals(input)){
num = Float.parseFloat(input); // parse inside loop if its float value
sum += num;
counter ++;
average = sum / counter;
input = scanner.nextLine(); // get next input at the end
}
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}
import java.util.Scanner;
public class AverageMark{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int mark[] = { 0, 0, 0, 0 };
System.out.printf("Enter integer mark %s between 0 and 100: ", i + 1);
}
System.out.println("Thanks for entering your marks.\n");
double average = (mark[0] + mark[1] + mark[2] + mark[3]) * 0.25;
String grade;
if (average >= 90) {
grade = "A+";
} else if (average >= 80) {
grade = "A";
} else if (average >= 70) {
grade = "B";
} else if (average >= 60) {
grade = "C";
} else if (average >= 50) {
grade = "D";
} else
grade = "F";
System.out.printf("Your average is: %.2f\n", average);
System.out.printf("Your average of %.2f has a letter grade of %s", average, grade);
}
}
//Hi sorry about the prior edit, I am new to this site. This is the code I wrote but I'm not sure why it won't run. It seems something might be missing but I'm
quite unclear. Please help thank you
Your code is almost there, just missing to store the values entered by user :
Store the marks in the array :
System.out.println("Enter the marks : ");
for (int i =0; i < mark.length; i ++) {
System.out.printf("Enter integer mark %s between 0 and 100: ", i + 1);
// assign each input to an index of the array
mark[i] = input.nextInt();
}
I would also advice you to declare the array like below rather than just initialing all the indexes to 0
int mark[] = new int[4];
The rest of your code works as it is.
There are some issues with the code.
The first error you see is:
AverageMark.java:13: error: <identifier> expected
System.out.println("Thanks for entering your marks.\n");
This is because you have a closing curly brace where it is not needed. The Java compiler is having difficulty interpreting the structure of your program. Remove line 11. You need to have matching curly braces to form a syntactically correct program. Did you mean to include a for loop ?
Once you correct that and recompile you'll find that:
AverageMark.java:10: error: cannot find symbol
System.out.printf("Enter integer mark %s between 0 and 100: ", i + 1);
symbol: variable i location: class AverageMark 1 error
Add the definition for int i=0 before the reference to i on line 10.
It should then compile and you can start debugging.
In below program , I am giving flexibility to user to enter number of subjects. Scanner allows the user to read values of various types. System.in: An InputStream which is typically connected to keyboard input of console program.
import java.util.Scanner;
class AverageMarks
{
public static void main(String args[])
{
int i;
System.out.println("Enter number of subjects");
Scanner sc=new Scanner(System.in);
//Here we are taking number of subjects from user
int n=sc.nextInt();
//Set array to no of subject
int[] a=new int[n];
double avg=0;
System.out.println("Enter marks");
// Taking marks from user for n no of subjects
for( i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
// Calculating total marks of subjects in avg variable
for( i=0;i<n;i++)
{
avg=avg+a[i];
System.out.println("Total marks of subjects : => "+avg);
}
//Calculating average % using total/no of subject formula
for(i=0;i<n-1;i++)
{
System.out.print(a[i]+",");
}
System.out.println(a[i]+") ="+avg/n);
}
}
This is my code that calculates ISBN 13th number but I seem to be having trouble. It keeps giving me an error on the return about invalid character constant and every time I change it, it gives an error on the method name I don't understand why.
import java.util.Scanner;
public class ISBN {
public static int VerifyISBN(String isbn) {
if(isbn.matches("[0-9]+") && isbn.length() > 12){
for(int i = 0; i < 12; i++){
char digit = isbn.charAt(i);
int sum = 0;
if (Character.isDigit(digit)){
int digitValue = digit - '0';
if(i % 2 == 0)
sum += digitValue;
else sum += 3 * digitValue;
}
else
return 'invalid'; (This is where I get the error)
}
}
}
public static void main(String[] args) {
final String TITLE = "ISBN-13 Identifier";
System.out.println("Welcome to the " + TITLE);
Scanner input = new Scanner(System.in);
String response;
do {
System.out.print("Enter the first 12 digits of an ISBN-13: ");
String isbn = input.nextLine().trim();
//String isbnVerifier = generateISBN(isbn);
//if(isbn.equals("INVALID"));
System.out.println("The 13th number of" + isbn + " is " +
((verifyISBN(isbn))));
System.out.print("Do this again? [nY]");
response = input.nextLine().toUpperCase();
} while (!response.equals("N"));
input.close();
System.out.println("Thank you for using the " + TITLE);
}
}
Two problems:
The literal 'invalid' is incorrect Java syntax. A string is delimited with double quotes. Single quotes are used to delimit single-character literals, such as 'a' but cannot be used for strings of characters.
The method is declared to return an integer, so you cannot return a String.
If your intent is to return a sentinel value indicating that the input was invalid, you should probably use something like -1, which can then be interpreted by the caller as the error condition.
Or, you could define the method to throw an exception.
First off, yes this a HW assignment. Having issues with recursive factorials in Java. Everything I'm finding on here and elsewhere already shows me what I've done is correct. However I'm having issues with an additional step. Basically what I need is the 1) User to enter a number 2) Factorial to be calculated 3) If user enters anything but a character or string (rather than an int) for an error message to come out 4) The question to repeat until user enters "0" to exit.
Steps 1 and 2 I have completed. I'm having issues with step 3. It seems like I am missing a return statement if the user enters anything but an int but I can't seem to figure out exactly what.
Here is code thus far:
import java.util.Scanner;
public class Recursive
{
public static void main(String[] args)
{
int number; // To hold a number
char letter; // To hold a character
//Create a Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//Get a number from the user
System.out.print("Enter an integer to find the factorial: ");
number = keyboard.nextInt();
//Display the factorial
System.out.println(number + "! is " + factorial(number));
}
private static int factorial(int n)
{
if (n == 0)
return 1; // Base Case
else if (n > 0)
return n * factorial(n-1);
else (!(n>0))
return
System.out.println(number + "is invalid");
}
}
After getting the user input, before doing factorial, we have to check if input is a number or not. We can use pattern. Check regular expression patterns to do that. After checking if it is a number or not, check if it is zero, if yes use exit (0) to come out of the program. If not do the factorial
while (true) {
// Get a number from the user
System.out.print("Enter an integer to find the factorial: ");
int number = keyboard.nextInt();
if (Pattern.matches("\\d+", String.valueOf(number))) {
if (Integer.valueOf(number) == 0)
System.exit(0);
// Display the factorial
System.out.println(number + "! is " + factorial(number));
}
else
System.out.println("Error");
}
My answer is based on an assumption that your factorial function is working properly.In order to complete your step 3 and 4 you need to take input in a loop. In that loop, take input as string and parse it into integer, use try catch so that you can catch exception when a non-integer is given as input and you can prompt an error message.
public static void main(String[] args)
{
Integer number; // To hold a number
String letter; // To hold a character
//Create a Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//Get a number from the user
System.out.print("Enter an integer to find the factorial: ");
while(keyboard.hasNext()){
letter = keyboard.next();
try{
number = Integer.parseInt(letter);
if(number==0){
//Exiting
break;
}
int fact = factorial(number);
//Display the factorial
System.out.println(number + "! is " + fact);
System.out.print("Enter an integer to find the factorial: ");
}
catch(NumberFormatException e){
System.out.println("Invalid input please enter integers only");
}
}
}
Also your factorial function is having compilation issues currently. You need to fix it for proper functioning of your code.
My solution for recursive factorial using Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.*;
class Main {
public static String factorial(int n,String s){
if(n>0){
BigInteger fact = new BigInteger(s);
fact = fact.multiply(new BigInteger(n + ""));
return factorial(n-1,fact.toString());
}
else{
return s.toString();
}
}
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int n = Integer.parseInt(line);
if(n==0)
System.out.println("Factorial is 0");
else{
String s = factorial(n,"1");
System.out.println("Factorial is " + s);
}
}
}
the example of factorial using recursive in Java
public class MainClass {
public static void main(String args[]) {
for (int counter = 0; counter <= 10; counter++){
System.out.printf("%d! = %d\n", counter,
factorial(counter));
}
}
public static long factorial(long number) {
if (number <= 1)
return 1;
else
return number * factorial(number - 1);
}
}
I am allowing the user to enter numbers via command line. I would like to make it so when the user enters more then one number on the command line at a time it displays a message asking for one number then press enter. then carries on.
here is my code. If someone could show me how to implement this I would appreciate it.
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
class programTwo
{
private static Double calculate_average( ArrayList<Double> myArr )
{
Double sum = 0.0;
for (Double number: myArr)
{
sum += number;
}
return sum/myArr.size(); // added return statement
}
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
ArrayList<Double> myArr = new ArrayList<Double>();
int count = 0;
System.out.println("Enter a number to be averaged, repeat up to 20 times:");
String inputs = scan.nextLine();
while (!inputs.matches("[qQ]") )
{
if (count == 20)
{
System.out.println("You entered more than 20 numbers, you suck!");
break;
}
Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input
try{
myArr.add(scan2.nextDouble());
count += 1;
System.out.println("Please enter another number or press Q for your average");
}
catch (InputMismatchException e) {
System.out.println("Stop it swine! Numbers only! Now you have to start over...");
main(args);
return;
}
inputs = scan.nextLine();
}
Double average = calculate_average(myArr);
System.out.println("Your average is: " + average);
}
}
As suggested in the comments to the question: Just do not scan the line you read for numbers, but parse it as a single number instead using Double.valueOf (I also beautified the rest of your code a little, see comments in there)
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
ArrayList<Double> myArr = new ArrayList<Double>();
int count = 0;
System.out.println("Enter a number to be averaged, repeat up to 20 times:");
// we can use a for loop here to break on q and read the next line instead of that while you had here.
for (String inputs = scan.nextLine() ; !inputs.matches("[qQ]") ; inputs = scan.nextLine())
{
if (count == 20)
{
System.out.println("You entered more than 20 numbers, you suck!");
break;
}
try{
myArr.add(Double.valueOf(inputs));
count++; //that'S even shorter than count += 1, and does the exact same thing.
System.out.println("Please enter another number or press Q for your average");
}
catch (NumberFormatException e) {
System.out.println("You entered more than one number, or not a valid number at all.");
continue; // Skipping the input and carrying on, instead of just starting over.
// If that's not what you want, just stay with what you had here
}
}
Double average = calculate_average(myArr);
System.out.println("Your average is: " + average);
}
(Code untested, so there may be errors in there. Please notify me if you got one ;))
String[] numbers = inputs.split(" ");
if(numbers.length != 1){
System.out.println("Please enter only one number");
}