java.lang.Integer.parseInt(Unknown Source) - java

line "int yes = Integer.parseInt(sc.nextLine());" is causing me problems in my calculator. im quite new at eclipse so any suggestions on correcting the code will be great.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your first number");
int nr1 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your second number");
int nr2 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your sign (+ , - , /, *)");
int yes = Integer.parseInt(sc.nextLine());
int ans =0;
int j = 0;
int reset =j;
java.lang.String anvin = sc.nextLine();
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
System.out.println(ans);
}
if(anvin.equalsIgnoreCase("yes")) {
return;
}
}
}

Try replacing one of these lines where you enter a number.
String anvin = sc.nextLine();
with
int anvinNumbers = sc.nextInt(); //for example
It may not answer why your parsing of text isnt working but is hopefully a solution.
Refactored code
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //scanner object created
int ans = 0;
//Inputs
System.out.println("Enter your first number");
int nr1 = sc.nextInt();
System.out.println("Enter your second number");
int nr2 = sc.nextInt();
System.out.println("Enter your sign (+ , - , /, *)");
String anvin = sc.nextLine();
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
}
System.out.println(ans);
}
}
}

Related

How do you declare n consecutive variables using a scanner and while loop?

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc != 0)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
So my intended goal is to have a script that will allow me to add two integers (chosen by user input with a scanner) and once those two are added i can then start a new sum. I'd also like to break from my while loop when the user inputs 0.
I think my error is that i can't use the != operator on the Scanner type Could someone explain the flaw in my code? (I'm used to python which is probably why I'm making this mistake)
You need to declare the variable out of while scope and update it until condition is not met
Try this:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int firstNum = 1;
int secondNum = 1;
while(firstNum !=0 && secondNum != 0)
{
System.out.println("first number: ");
firstNum = sc.nextInt();
System.out.println("second number: ");
secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
You should have some kind of an "infinite" loop like so:
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
if (firstNum == 0) {
break;
}
System.out.println("second number: ");
int secondNum = sc.nextInt();
if (secondNum == 0) {
break;
}
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
Hi just remove the while block since it has no sence to use it
Here the corrected code
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
You cannot compare the Object sc with an integer value 0. You can do the below code.
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
System.out.println("first number: ");
int firstNum = sc.nextInt();
while(firstNum != 0)
{
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
System.out.println("first number: ");
firstNum = sc.nextInt();
}
}
}
or
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
while(true)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
if(firstNum == 0) {
break;
}
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
System.out.println("first number: ");
firstNum = sc.nextInt();
}
}
}
It's ugly af but it will let you understand the process
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean continueRunning = true;
while (continueRunning) {
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
continueRunning = firstNum != 0 && secondNum != 0;
}
}
}
also ugly but i will sleep better tonight.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
requestNumbersAndSum(scanner);
}
private static void requestNumbersAndSum(Scanner scanner) {
int firstNum = requestANum(scanner, "first number: ");
int secondNum = requestANum(scanner, "second number: ");
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
requestNumbersAndSum(scanner);
}
private static int requestANum(Scanner scanner, String messageToUser) {
System.out.println(messageToUser);
int requestedNumber = scanner.nextInt();
if(requestedNumber == 0){
System.exit(0);
}
return requestedNumber;
}
}

Use of Private static

Can you find the source of error in this?
package calc;
import java.util.Scanner;
public class Calc {
Scanner scan = new Scanner(System.in);
public void add() {
System.out.println("Enter 1st number");
int s1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int s2 = scan.nextInt();
scan.nextLine();
int sum = s1 + s2;
System.out.println("The sum is: " + sum);
}
public void diff() {
System.out.println("Enter 1st number");
int d1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int d2 = scan.nextInt();
scan.nextLine();
int diff = d1 - d2;
System.out.println("The difference is: " + diff);
}
public void prod() {
System.out.println("Enter 1st number");
int p1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int p2 = scan.nextInt();
scan.nextLine();
int prod = p1 + p2;
System.out.println("The product is: " + prod);
}
public void quo() {
System.out.println("Enter 1st number");
int q1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int q2 = scan.nextInt();
scan.nextLine();
int quo = q1 + q2;
System.out.println("The quotient is: " + quo);
}
public static void main(String[] args) {
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
char ans = 0;
System.out.println("Calculator");
System.out.println("1.Addition\n" + "2.Subtraction\n" + "3.Multiplication\n" + "4.Division\n" + "Enter operation number:");
int n1 = scan.nextInt();
scan.nextLine();
switch (n1) {
case 1:
op.add();
break;
case 2:
op.diff();
break;
case 3:
op.prod();
break;
case 4:
op.quo();
break;
default:
System.out.println("Invalid input");
break;
}
System.out.println("Try again? [Y/N]");
ans = scan.nextLine().charAt(0);
} while (ans == 'Y' || ans == 'y');
}
}
and then netbeans has this auto correct that resulted into this:
package calc;
import java.util.Scanner;
public class Calc {
private static char ans;
it added a "private static char ans;" and I would like to understand more how did that fix my code. Thanks
ans is defined within the do{ ... } while() loop but it must be defined outside, to make it available for condition in the while.
So do:
char ans = 0;
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
ans = 0;

How to modify this code to print the 3 integers in order?

I have a code that prints the biggest integer between 3 integers and I want to sort these 3 integers (like num < num1 < num2).
I want to modify my code to achieve this, how can I do it?
import java.util.Scanner;
public class digits {
public static void main(String[] args) {
int num = 0;
int num1 = 0;
int num2 = 0;
int big = 0;
System.out.println("Please insert 3 numbers: ");
Scanner number = new Scanner(System.in);
if (number.hasNextInt()) {
num = number.nextInt();
big = num;
}
if (number.hasNextInt()) {
num1 = number.nextInt();
if (num1 > num) {
big = num1;
}
}
if (number.hasNextInt()) {
num2 = number.nextInt();
if (num2 > num && num2 > num1) {
big = num2;
}
System.out.println(big + ">" + num1 + ">" + num);
} else {
System.out.println("Error: Invalid Value.");
}
}
}
Try below code
public static void main(String[] args) {
System.out.println("Please insert 3 numbers: ");
Scanner number = new Scanner(System.in);
Integer[] input = new Integer[3];
int i = 0;
while (i != 3) {
input[i++] = number.nextInt();
}
number.close();
Arrays.sort(input, Collections.reverseOrder());
StringBuffer sb = new StringBuffer();
for (Integer a : input) {
sb.append(a).append(">");
}
System.out.println(sb.substring(0, sb.length() - 1));
}
if(num1>num)
{
big=num1;
}
Change this to:
if(num1>num)
{
big=num1;
}
else
{
// swap num1 and num
int tmp = num;
num = num1;
num1 = num;
}
Also change
if(num2>num && num2>num1)
{
big=num2;
}
to
if(num2>num1)
{
big=num2;
}
else
{
// num1 is largest, so swap num1 and num2
int tmp = num1;
num1 = num2;
num2 = tmp;
if(num > num1){
//swap num and num1
int t = num1;
num1 = num;
num = t;
}
}
Look at your code: you're choosing the biggest of the three numbers (big), but you never change num1 or num.
So, your result will always be: greater number>second value from System.in>third value from System.in.
In other words, you're sorting only the higher value.
Something like this should work
import java.util.Scanner;
public class digits {
public static void main(String[] args)
{
int[] nums = new int[3];
int k = 0;
System.out.println("Please insert 3 numbers: ");
Scanner number = new Scanner (System.in);
while (number.hasNext() && k < 3) {
int i = 0;
try {
i = Integer.parseInt(number.nextLine());
}
catch (NumberFormatException e) {
System.out.println("Error: Invalid Value.");
}
nums[k] = i;
k++;
}
Arrays.sort(nums);
System.out.println(nums[2]+ ">" +nums[1]+ ">" +nums[0]);
}
}

Why does my calculator automatically answer "0" before i can put my sign in?

Somewhere around line 15 it gives me issues.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //scanner object created
System.out.println("Enter your first number");
int nr1 = sc.nextInt();
System.out.println("Enter your second number");
int nr2 = sc.nextInt();
System.out.println("Enter your sign (+ , - , /, *)");
String anvin = sc.nextLine();
int ans = 0;
//somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
}
System.out.println(ans);
System.out.println("To continue type yes");
String yes= sc.nextLine();
if(yes.equalsIgnoreCase("yes")) {
return;
}
}
}
it answers "0" whatever I enter before I can put in my sign
Enter your first number
9
Enter your second number
9
Enter your sign (+ , - , /, *)
0
To continue type yes
please tell me what I did wrong and possibly correct it so I can understand further
Try changing your sc.nextInt() lines to Integer.parseInt(sc.nextLine()). This should make your code work correctly.
EDIT: updated the code to include a while loop to make it so you can do multiple runs per your comment. This would also require you changing your last if statement to break; instead of return;
Scanner sc = new Scanner(System. in ); //scanner object created
while (true) {
System.out.println("Enter your first number");
int nr1 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your second number");
int nr2 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your sign (+ , - , /, *)");
String anvin = sc.nextLine();
int ans = 0;
//somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
if (anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
} else if (anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
} else if (anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
} else if (anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
}
System.out.println(ans);
System.out.println("To continue type yes");
String yes = sc.nextLine();
if (!yes.equalsIgnoreCase("yes")) {
break;
}
}
Change
String anvin = sc.nextLine();
to
String anvin = sc.next();
Also keep in mind that you might divide through zero ;-)
Edit:
also change
String yes= sc.nextLine();
to
String yes= sc.next();
Instead of sc.nextLine(); use sc.next();
I would suggest you this, you can not only learn using objects but learn a better way of writing managed codes too,
Calculator.java -> a class
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
//Instantiate
Scanner input = new Scanner(System.in);
Calculations calc = new Calculations();
// Variable declarations
double answer = 0, entry1 , entry2 ;
char operator;
// Start
System.out.println("***** Welcome to the Command line calculator program *****");
System.out.print("Please enter the first number :");
entry1 = input.nextDouble();
System.out.print("Please enter the second number:");
entry2 = input.nextDouble();
System.out.println("Please enter the operation : ");
System.out.println("***** Operations :- + -> Add ; - ->Substract ; / -> Divide ; * -> Multiply ; ^ : Power *****");
operator = input.next().charAt(0);
// Switch case
switch (operator){
case '+' : answer = calc.add(entry1, entry2);
break;
case '-' : answer = calc.substract(entry1, entry2);
break;
case '/' : answer = calc.divide(entry1, entry2);
break;
case '*' : answer = calc.multiply(entry1, entry2);
break;
case '^' : answer = calc.power(entry1, entry2);
break;
}
System.out.println(entry1 + " " + operator + " " + entry2 + " = " + answer);
}
}`
Calculations.java -->another class holding calculations
import java.math.*;
public class Calculations {
// Addition Method
double add (double first, double second){
double answer = first + second;
return answer;
}
// Substraction Method
double substract (double first, double second){
double answer = first - second;
return answer;
}
// Multiplication Method
double multiply (double first, double second){
double answer = first * second;
return answer;
}
// Division Method
double divide (double first, double second){
double answer = first / second;
return answer;
}
// Power Method
double power(double a, double b){
double answer =Math.pow(a, b);
return answer;
}
}

Java: Ask for continue

I'm trying to make a calculator. These operators: x, +, -, / work fine.
But I want the user to be able to do 2 things after he gets the answer on his math problem.
Ask user if he wants to continue.
If user types in yes he gets to put in 2 numbers that it counts again.
If the user types no just shut down.
Here's my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Minscanner = new Scanner(System.in);
int nr1 = Integer.parseInt(Minscanner.nextLine());
int nr2 = Integer.parseInt(Minscanner.nextLine());
int yes = Integer.parseInt(Minscanner.nextLine());//trying to fix reset
int ans =0;
int reset = J;/trying to make it reset if user types in yes
String anvin = Minscanner.nextLine();
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
System.out.println(ans);
}
if(anvin.equalsIgnoreCase("yes")) {
return;
}
}
}
Put your code in a
do {
...
} while (condition);
loop, and in your case the condition would be something like wantToContinue if user say "yes".
Then the program will not end unless user no longer wants to calculate.
You can refactor your code as bellow. This may help you
boolean status=true;
while (status){
Scanner scanner = new Scanner(System.in);
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter your two numbers one by one :\n");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("Enter your operation you want to perform ? ");
int ans =0;
String option = scanner1.nextLine();
if(option.equalsIgnoreCase("+")) {
ans = num1 + num2;
}
else if(option.equalsIgnoreCase("-")) {
ans = num1 - num2;
}
else if(option.equalsIgnoreCase("*")) {
ans = num1 * num2;
}
else if(option.equalsIgnoreCase("/")) {
ans = num1 / num2;
}
System.out.println(ans);
System.out.println("you want to try again press y press j for shutdown\n");
Scanner sc = new Scanner(System.in);
String input=sc.nextLine();
if (input.equalsIgnoreCase("J")) {
System.exit(0);
} else if (input.equalsIgnoreCase("Y")) {
status = true;
}
}

Categories