Calculator no work [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.
import java.util.Scanner;
public class Calcu {
public static void main( String[] args )
{
Scanner mati = new Scanner(System.in);
System.out.println("This program adds up or substracts two numbers");
System.out.println("Enter an operator");
String letter = mati.next(); //WAITS FOR THE PHRASE ADD OR SUBSTRACT
System.out.println("Enter your first number");
int userNumberone = mati.nextInt(); // Get's first Number
System.out.println("Enter your second number");
int userNumbertwo = mati.nextInt(); //Get's Following Number
if(letter == "add") {
int result = userNumberone + userNumbertwo;
System.out.println(result);
} else if(letter == "substract") {
int result1 = userNumberone - userNumbertwo; //If statement to add or substract.
System.out.println(result1);
}
}
}

You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?

Related

Why are my code not working ?.This a code to check whether a number is a palindrome or not for positive number [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
import java.util.Scanner;
public class CheckPalindrome3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your number :");
String num = input.nextLine();
String arr = "";
for(int i = num.length() - 1 ; i >= 0;i--)
{
arr = arr + num.charAt(i);
}
System.out.println(arr+"\n"+num);
if(arr == num) {
System.out.println("Yes that's a Palindrome !!");
}
else
{
System.out.println("No that's not a Palindrome !!");
}
}
}
When I put 121 it printed out No thats not a palindrome but arr is the same as num
The problem is the test arr == num. For strings, == tests whether the two strings have the same address in memory (which often is not true, even if the strings look the same). Use arr.equals(num) instead. That will test if all of the characters in arr are the same as those in num.

Why do i get a NumberFormatException error? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
So the code is supposed to continually give fibonacci values for any given number until you enter "q", but for some reason instead of stopping, I get an error, anyone know why? I know that if you try to parse a string, it will cause the error but I have "if(input != "q")" to stop that so why is it still happening?
Tester class:
import java.util.Scanner;
public class FibonacciTester
{
public static void main(String[] args)
{
String input = "1";
System.out.println("Input a positive integer that you want to find the fibonacci number for");
System.out.println("Type 'q' to quit");
Scanner in = new Scanner(System.in);
while (input != "q"){
System.out.print("Type in the positive integer");
input = in.next();
if(input != "q"){
int number = Integer.parseInt(input);
FibonacciV fibonacci = new FibonacciV(number);
number = FibonacciV.fibonacci(number);
System.out.println("The Fibonacci number: " + number);
}
}
}
}
Class
public class FibonacciV
{
FibonacciV(int x)
{
}
public static int fibonacci(int x)
{
if (x == 0) //Base case
{
return 0;
}
else if (x == 1) //Second base case
{
return 1;
}
else
{
return fibonacci(x-1)+ fibonacci(x-2); // recursive call
}
}
}
Change input != "q" to !input.equals("q").
Read more about how to compare strings in java.
You are getting NumberFormatException because it's running Integer.parseInt(input) when the input is "q", which is not a number. And the code was able to reach this statement because your string comparison is incorrect.

why wont my while loop break [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I can not figure out why my while loop won't break and it just keeps running I have tries making Lc 10 and I tried return but nothing will end the loop.
import java.util.Scanner;
public class BirthdayReminder
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] BDay = new String[10];
String[] friend = new String[10];
int Lc = 0;
String i;
while(Lc < 10) {
System.out.println("enter a friends name or zzz to quit");
i = input.nextLine();
if(i == "zzz") {
break;
}
else if(i != "zzz"){
friend[Lc] = i;
System.out.println("enter their birthday.");
i = input.nextLine();
BDay[Lc] = i;
Lc++;
return;
}
}
System.out.println("hi");
}
}
You need to check string equality using equals(), not ==. Neither block of code inside the if is getting entered, so Lc is never incremented.

DoWhile in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When I try to do a dowhile loop in Java with a string it just terminates the program without an error not giving me a chance to enter if I would like to continue. How do I make my program loop if requested using a string?
public static void main(String args[]) {
Scanner user_input = new Scanner (System.in);
Maths math = new Maths();
double firstNum, secondNum;
String calcAgain = null;
do{
System.out.println("Enter First Number: ");
firstNum = user_input.nextDouble();
System.out.println("Enter Second Number: ");
secondNum = user_input.nextDouble();
System.out.println("Please select an operation! (+,-,*,/)");
String operation = user_input.next();
if (operation.equals("+")) {
System.out.println(math.add(firstNum, secondNum));
}
else if (operation.equals("-")) {
System.out.println(math.subtract(firstNum, secondNum));
}
else if (operation.equals("*")) {
System.out.println(math.multiply(firstNum, secondNum));
}
else if (operation.equals("/")) {
System.out.println(math.divide(firstNum, secondNum));
}
else {
System.out.println("Invalid Function!");
}
System.out.println("Would you like to do another calculation? ");
calcAgain = user_input.nextLine();
}while(calcAgain == "yes");
You might want to look at string operations for equality. The condition while(calcAgain == "yes") will not execute ever, because you are comparing the object itself. String equality is done with the .equals() function.

String answer1 = "yes"; if (answer1 == "yes") gives false. Why? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Equal strings aren't equal (==) in Java? [duplicate]
(3 answers)
Closed 9 years ago.
import java.util.Scanner;
public class NewClass {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name");
String name = scanner.nextLine();
System.out.println("Is "+ name + " really your name?");
String answer1 = scanner.nextLine();
if (answer1 == "yes"){
System.out.println("Alright ");
}else {System.out.println("Liar!");
}
}
}
It outputs Liar! even though I typed yes, so that answer1 equals yes. Why?
Because with == you are testing reference equality, not value equality. Good reading.
Instead answer1 == "yes" do answer1.equals("yes").

Categories

Resources