I am getting this error:
Error: reached end of file while parsing
I know it means I need to close the curly bracket somewhere, but I have tried everything. I figured line 45 is the brace that needs to be closed, but I'm not sure how. This is a program for helping find the smallest number of coins needed to make the integer inputted by the user. For example, 37 would yield 2 Quarters, 1 Dime, and 2 pennies.
import java.util.Scanner;
public class Change {
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
Int n = sc.nextInt("Enter a positive integer" );
int number1, number2; // Division operands
int quotient; // Result of division
{if (QtrCnt > 0)
if (QtrCnt > 1)
System.out.println(QtrCnt + " quarters");
else
System.out.println(QtrCnt + " quarter");
}
if (DimeCnt > 0)
{
if (DimeCnt > 1)
System.out.println(DimeCnt + " dimes");
else
System.out.println(DimeCnt + " dime");
}
if (NicklCnt > 0)
{
if (NicklCnt > 1)
System.out.println(NicklCnt + " nickles");
else
System.out.println(NicklCnt + " nickle");
}
if (PennyCnt > 0);
{
if (PennyCnt > 1);
System.out.println(PennyCnt + " pennies");
System.out.println(PennyCnt + " penny");
}
int q = 25;
int d = 10;
int n = 5;
int p = 1;
if (a < 0);
System.out.println("ERROR");
{
String (money >=25); { int numQuarters = money/ 25; }
money -= numQuarters * 25;
QtrCnt = (num1 - num1 % 25) / 25;
num1 = num1 - QtrCnt * 25;
String(money >=10); { int numDimes = money/ 10; }
money -= numDimes * 10;
DimeCnt = (num1 - num1 % 10) / 10;
num1 = num1 - DimeCnt * 10;
String (money >=5); { int numNickles = money/ 5; }
money -= numNickles * 5;
NicklCnt = (num1 - num1 % 5) / 5;
num1 = num1 - NicklCnt * 5;
String (money >=1); { int numPennies = money/ 1; }
money -= numPennies * 1;
PennyCnt = (num1 - num1 % 1) / 1;
num1 = num1 - PennyCnt * 1;
}
}
Yes, you are in fact missing the closing bracket for the class.
You also have a lot of brackets that basically don't do anything. Remember that if you don't put a { immediately after the () of an if, it won't belong to the if statement.
I cleaned up your code a little and got this:
import java.util.Scanner;
public class Change
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
Int n = sc.nextInt("Enter a positive integer" );
int number1, number2; // Division operands
int quotient; // Result of division
{ // <- Won't do anything
if (QtrCnt > 0)
if (QtrCnt > 1)
System.out.println(QtrCnt + " quarters");
else
System.out.println(QtrCnt + " quarter");
}
if (DimeCnt > 0)
{
if (DimeCnt > 1)
System.out.println(DimeCnt + " dimes");
else
System.out.println(DimeCnt + " dime");
}
if (NicklCnt > 0)
{
if (NicklCnt > 1)
System.out.println(NicklCnt + " nickles");
else
System.out.println(NicklCnt + " nickle");
}
if (PennyCnt > 0);
{
if (PennyCnt > 1);
System.out.println(PennyCnt + " pennies");
System.out.println(PennyCnt + " penny");
}
int q = 25;
int d = 10;
int n = 5;
int p = 1;
if (a < 0);
System.out.println("ERROR");
{ // <- Won't do anything
String (money >=25); { int numQuarters = money/ 25; }
money -= numQuarters * 25;
QtrCnt = (num1 - num1 % 25) / 25;
num1 = num1 - QtrCnt * 25;
String(money >=10); { int numDimes = money/ 10; }
money -= numDimes * 10;
DimeCnt = (num1 - num1 % 10) / 10;
num1 = num1 - DimeCnt * 10;
String (money >=5); { int numNickles = money/ 5; }
money -= numNickles * 5;
NicklCnt = (num1 - num1 % 5) / 5;
num1 = num1 - NicklCnt * 5;
String (money >=1); { int numPennies = money/ 1; }
money -= numPennies * 1;
PennyCnt = (num1 - num1 % 1) / 1;
num1 = num1 - PennyCnt * 1;
}
}
// <- This is where you're missing the class bracket
Please try to improve the way you're formating your code because it really helps finding these kinds of mistakes very quickly.
Keep in mind that I only reformated the code, I did not change what it does or check if it even works.
Related
import java.util.Scanner;
public class MoneyChange {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Input an amount of money: ");
double value = reader.nextDouble();
int money = (int) value;
int thousand = money / 1000;
int fivehundred = money / 500;
int hundred = money / 100;
int fifty = (money % 100) / 50;
int twenty = ((money % 100) % 50) / 20;
int ten = (((money % 100) % 50) % 20) / 10;
int five = ((((money % 100) % 50) % 20) % 10) / 5;
int one = (((((money % 100) % 50) % 20) % 10) % 5) / 1;
System.out.println("Number of 1000-Bhat Banknote(s) is " + thousand);
System.out.println("Number of 500-Bhat Banknote(s) is " + fivehundred);
System.out.println("Number of 100-Bhat Banknote(s) is " + hundred);
System.out.println("Number of 50-Bhat Banknote(s) is " + fifty);
System.out.println("Number of 20-Bhat Banknote(s) is " + twenty);
System.out.println("Number of 10-Bhat Coin(s) is " + ten);
System.out.println("Number of 5-Bhat Coin(s) is " + five);
System.out.println("Number of 1-Bhat Coin(s) is " + one);
reader.close();
}
}
I would like to know how to continue the values from the first variable onto the other and not start again. Help me out!
In this code
int fifty = (money % 100) / 50;
you can calculate money % 100 and store it in a variable you can re-use it
int hun = money % 100;
int fifty = (hun) / 50;
Also be aware of Integer division
please do small modifications in your program.please take the look below code once.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Input an amount of money: ");
double value = reader.nextDouble();
int money = (int) value;
int thousand = money / 1000;
int remain = money % 1000 ;
int fivehundred = remain / 500;
remain = remain % 500;
int hundred = remain / 100;
remain = remain % 100;
int fifty = remain / 50;
remain = remain % 50;
int twenty = remain / 20;
remain = remain % 20;
int ten = remain / 10;
remain = remain % 10;
int five = remain / 5;
remain = remain % 5;
int one = remain / 1;
System.out.println("Number of 1000-Bhat Banknote(s) is " + thousand);
System.out.println("Number of 500-Bhat Banknote(s) is " + fivehundred);
System.out.println("Number of 100-Bhat Banknote(s) is " + hundred);
System.out.println("Number of 50-Bhat Banknote(s) is " + fifty);
System.out.println("Number of 20-Bhat Banknote(s) is " + twenty);
System.out.println("Number of 10-Bhat Coin(s) is " + ten);
System.out.println("Number of 5-Bhat Coin(s) is " + five);
System.out.println("Number of 1-Bhat Coin(s) is " + one);
reader.close();
}
I need to make this java code repeat based on the user input, and I cannot make it repeat using the code that I do have so far. I am not supposed to use any other imports besides the scanner and use class main. This is because we are using the https://repl.it/languages/java10 as our compiler because we are an elementary class. When I run the code, it is supposed to ask ten random addition and subtraction and should ask if the user wants to continue or not. When entering 1 for continue, it should ask another ten questions. however, upon running this code, it stops after the first question.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Answer the following questions.");
Scanner in = new Scanner(System.in);
int A = 0;
int N = 10;
int n = 0;
int H = 0;
boolean p = true;
while (p){
int R1 = (int)(Math.random() * 50 + 1);
int R2 = (int)(Math.random() * 999 + 1);
int R3 = (int)(Math.random() * 999 + 1);
if(R1>25){
System.out.println( "" + R2 + " + " + R3);
A = in.nextInt();
if (A == (R2 + R3))
System.out.println("Correct");
else
System.out.println("Incorrect");
}
if(R1<25){
System.out.println( "" + R2 + " - " + R3);
A = in.nextInt();
if (A == (R2 - R3))
System.out.println("Correct");
else
System.out.println("Incorrect");}
N--;
if (N==0)
p = false;
continue;
}System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
H = in.nextInt();
if (H==1)
p=true;
else
p=false;
while (N>0);
}
}
That's why you put the question System.out.println("Do you want ot continue? Put 1 for yes, 2 for no."); out of the while.
I recommend use do while instead of while. So you just need to put the question inside of loop do while.
System.out.println("Answer the following questions.");
Scanner in = new Scanner(System.in);
int A = 0;
int N = 10;
int H = 0;
boolean p = true;
do{
int R1 = (int) (Math.random() * 50 + 1);
int R2 = (int) (Math.random() * 999 + 1);
int R3 = (int) (Math.random() * 999 + 1);
if (R1 > 25) {
System.out.println("" + R2 + " + " + R3);
A = in.nextInt();
if (A == (R2 + R3)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
if (R1 < 25) {
System.out.println("" + R2 + " - " + R3);
A = in.nextInt();
if (A == (R2 - R3)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
N--;
System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
H = in.nextInt();
if (H == 1) {
p = true;
} else {
p = false;
}
if (N == 0) {
p = false;
System.out.println("You have reached your max attempts.")
}
}while (N > 0 && p);
I've been working on this problem for the past two hours or so and I've encountered a roadblock. I can actually perform the division but when it comes time to print the remainder that isn't 0 the output doesn't match. I would like to know what is it what I'm doing wrong.
public class Division {
public static void main(String[] args) {
int numerator = 0;
int numeratorprint = 0;
int denominator = 0;
int product = 0;
int remainder = 0;
int counter = 1;
Scanner input;
input = new Scanner(System.in);
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
numeratorprint = numerator;
denominator = input.nextInt();
while ((numerator < 0) || (denominator < 0)) {
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
denominator = input.nextInt();
}
if (numerator == 0){
System.out.print(numerator + "/" + denominator + " = " + numerator + " with a remainder of " + numerator);
}
else if (denominator == 0){
System.out.print("This result is undefined (Cannot divide by Zero)");
}
else if (denominator > numerator){
System.out.print("Cannot do proper fractions");
}
else {
while (numerator > denominator){
counter++;
numerator = numerator - denominator;
}
}
product = counter * denominator;
remainder = numeratorprint - product;
System.out.println(numeratorprint + " / " + denominator + " = " + counter + " with a remainder of " + remainder);
}
}
/* Sample I/O 1
Enter two positive intergers for division:
25
5
OUTPUT:
25 / 5 = 5 with a remainder of 0
Sample I/O 2
Enter two positive intergers for division:
27
5
OUTPUT:
27 / 5 = 6 with a remainder of -3
*/
I believe you have an off by one error where you are starting your counter at 1 rather than 0. I tested a few numbers and changing this seems to fix it just fine. So change
int counter = 1;
to
int counter = 0;
Also, you must change your while loop conditional on the else block to be
while (numerator >= denominator) {...
in order to account for the case of when the divisor evenly divides the dividend.
I saw few errors in your code.
1. Redundant variable
2 why don`t you use modular operator ?
3. you can remove a while loop too
public class Division {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
System.out.print("Enter two positive intergers for division:\n");
int numerator = input.nextInt();
int numeratorprint = numerator;
int denominator = input.nextInt();
int quotient = 0;
int remainder = 0;
while ((numerator < 0) || (denominator < 0)) {
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
denominator = input.nextInt();
}
if (numerator == 0) {
System.out.print(numerator + "/" + denominator + " = " + numerator + " with a remainder of " + numerator);
} else {
if (denominator == 0) {
System.out.print("This result is undefined (Cannot divide by Zero)");
} else {
if (denominator > numerator) {
System.out.print("Cannot do proper fractions");
} else {
remainder = numerator % denominator;
quotient = ((numerator / denominator) - (remainder / denominator));
}
}
}
System.out.println(numeratorprint + " / " + denominator + " = " + quotient + " with a remainder of " + remainder);
}
}
This question already has answers here:
What is a NumberFormatException and how can I fix it?
(9 answers)
Closed 6 years ago.
I am working on a fraction calculator program for my AP Computer Science class. This code compiles and runs, however when I put in any input, besides quit, there is an error. With an input 8_9/4 + 3/7 there is an error of:
Exception in thread "main" java.lang.NumberFormatException: For input string: "4 + 3/7".
Can someone please help me figure out what's wrong?
import java.util.*;
public class FracCalc {
public static void main(String[] args) {
typeEquation();
}
public static void typeEquation() {
System.out.print("Enter an equation, or \"quit\" : ");
Scanner scan = new Scanner(System.in);
String fraction = scan.nextLine();
String secondOperator;
if (fraction.equalsIgnoreCase("quit")) {
finish();
}
else {
produceAnswer(fraction);
}
}
public static void produceAnswer(String fraction) {
int whole2;
int numerator2;
int denominator2;
int whole1;
int numerator1; //all caps identifier thing?
int denominator1;
String operator = fraction.substring((fraction.indexOf(" ")) + 1);
if (fraction.contains("_")) {
whole2 = Integer.parseInt(fraction.substring(0, fraction.indexOf("_")));
numerator2 = Integer.parseInt(fraction.substring(fraction.indexOf("_") + 1, fraction.indexOf("/")));
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
numerator2 = whole2 * denominator2 + numerator2;
}
else if (fraction.contains("/")) {
numerator2 = Integer.parseInt(fraction.substring(0,fraction.indexOf("/")));
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
}
else {
whole2 = Integer.parseInt(fraction.substring(0));
numerator2 = whole2;
denominator2 = 1;
}
if (fraction.contains("_")) {
whole1 = Integer.parseInt(fraction.substring(0, fraction.indexOf("_")));
numerator1 = Integer.parseInt(fraction.substring(fraction.indexOf("_") + 1, fraction.indexOf("/")));
denominator1 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
numerator1 = (whole1*denominator1) + numerator1;
}
else if (fraction.contains("/")) {
numerator1 = Integer.parseInt(fraction.substring(0, fraction.indexOf("/")));
denominator1 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
}
else {
whole1 = Integer.parseInt(fraction.substring(0));
numerator1 = whole1;
denominator1 = 1;
}
if (fraction.contains("_")) {
whole2 = Integer.parseInt(fraction.substring(0, fraction.indexOf("_")));
numerator2 = Integer.parseInt(fraction.substring(fraction.indexOf("_") + 1, fraction.indexOf("/")));
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
numerator2 = whole2 * denominator2 + numerator2;
}
else if (fraction.contains("/")) {
numerator2 = Integer.parseInt(fraction.substring(0, fraction.indexOf("/")));
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
}
else {
whole2 = Integer.parseInt(fraction.substring(0));
numerator2 = whole2;
denominator2 = 1;
}
if (operator.equals("+")) {
System.out.println(addingFractions(numerator1, numerator2, denominator1, denominator2));
}
else if (operator.equals("*")) {
System.out.println(multiplyingFractions(numerator1, numerator2, denominator1, denominator2));
}
else {
int x = numerator2;
int y = denominator2;
denominator2 = x;
numerator2 = y;
System.out.println(multiplyingFractions(numerator1, numerator2, denominator1, denominator2));
}
int dividend = (denominator1 * numerator2) + (numerator1 * denominator2);
int divisor = denominator1 * denominator2;
int remainder = dividend % divisor;
while (remainder != 0){
dividend = divisor;
divisor = remainder;
remainder = dividend % divisor;
}
}
public static String multiplyingFractions(int numerator1, int numerator2, int denominator1, int denominator2) {
int newNumerator = numerator1 * numerator2;
int newDenominator = denominator1 * denominator2;
int divisor = reducingFractions(newNumerator, newDenominator);
newNumerator /= divisor;
newDenominator /= divisor;
int integerComponent = 0;
while (newNumerator >= newDenominator) {
integerComponent++;
newNumerator -= newDenominator;
}
String answer = "";
if (integerComponent > 0) {
answer += integerComponent + "_";
}
if (newNumerator != 0) {
answer += reducingFractions(newNumerator, newDenominator);
}
return answer;
}
public static String addingFractions(int numerator1, int numerator2, int denominator1, int denominator2) {
int newNumerator = (numerator1 * denominator2) + (numerator2 * denominator1);
int newDenominator = denominator1 * denominator2;
int divisor = reducingFractions(newNumerator, newDenominator);
newNumerator /= divisor;
newDenominator /= divisor;
int integerComponent = 0;
while (newNumerator >= newDenominator) {
integerComponent++;
newNumerator -= newDenominator;
}
String answer = "";
if (integerComponent > 0) {
answer += integerComponent + "_";
}
if (newNumerator != 0) {
answer += newNumerator + "/" + newDenominator;
}
return answer;
}
public static int reducingFractions(int newNumerator, int newDenominator) {
int newNumerator_abs = Math.abs (newNumerator);
int newDenominator_abs = Math.abs (newDenominator);
int minimumNumber = Math.min (newNumerator_abs, newDenominator_abs);
int divisor = 1;
for (int i = 1; i <= minimumNumber; i++) {
if (newNumerator % i == 0 && newDenominator % i == 0){
divisor = 1;
}
}
return divisor;
}
public static void finish() {
System.out.println("Goodbye!");
}
}
You are getting this error, because you try to parse a String as integer, even if it isn't an integer. This is the problematic line:
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
for this input 8_9/4 + 3/7.
This fraction.indexOf("/") returns you the first occurrence of the slash. So, the following expression fraction.substring(fraction.indexOf("/") + 1) returns 4 + 3/7 which is not an integer, so it cannot be parsed using Integer.parseInt.
If you want to get only 4 instead of 4 + 3/7, you can replace your line with:
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1, fraction.indexOf(" ")));
You did not extract the "fraction" string correctly. Be careful when you do Integer.parseInt(). It doesn't take in equation.
In the code that you wrote:
else if (fraction.contains("/")) {
numerator2 = Integer.parseInt(fraction.substring(0,fraction.indexOf("/")));
denominator2 = Integer.parseInt(fraction.substring(fraction.indexOf("/") + 1));
}
The fraction.substring(0,fraction.indexOf("/") will return "8_9" as string. Which is not recognisable by Integer.parseInt.
Try to run in debug and set the breakpoint to break at Interger.parseInt for visualising the values.
package chapter5;
import java.util.Scanner;
public class Exercise5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 10;
int one = (int) (Math.random() * (max - min)) + min;
int two = (int) (Math.random() * (max - min)) + min;
int num = (int) (4* Math.random()+1);
char operator = 0;
switch (num)
{
case 1: operator = '+';
break;
case 2: operator = '-';
break;
case 3: operator = '*';
break;
case 4: operator = '/';
break;
}
System.out.println("What is " + one + " " + operator + " " + two + "?");
double ans = 0;
double ans1 = 0;
ans = input.nextDouble();
if (num == 1) {
ans1 = one + two;
} else {
if (num == 2) {
ans1 = one - two;
} else {
if (num == 3 ) {
ans1 = one * two;
} else {
if (num == 4) {
ans1 = one / two;
}
}
}
if (ans == ans1) {
System.out.println("Correct!");
} else {
if (ans != ans1) {
System.out.println("incorrect");
}
}
}
}
}
So I am trying to create an application that generates a question, and calculates if your input is correct or incorrect, so far from all my tests, when it is correct it displays the correct thing, but when it is incorrect the app terminates.
I cleared your code up a little bit, it seems to run fine either way though. Perhaps you should check your editor settings to see if the output console doesn't clear when the program is finished executing.
package chapter5;
import java.util.Scanner;
public class Excercise5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 10;
int one = (int) (Math.random() * (max - min)) + min;
int two = (int) (Math.random() * (max - min)) + min;
int num = (int) (4 * Math.random());
char operator = "+-/*".charAt(num);
System.out.println("What is " + one + " " + operator + " " + two + "?");
double ans = 0;
double ans1 = 0;
ans = input.nextDouble();
switch(num) {
case 0:
ans1 = one + two;
break;
case 1:
ans1 = one - two;
break;
case 2:
ans1 = one / two;
break;
case 3:
ans1 = one * two;
break;
}
if (ans == ans1) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect");
}
}
}