This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am new to Java. I wrote this program and when I run it, it always gives me that the answer is 0.0
Can you tell me what I did wrong?
import java.util.Scanner;
public class Learnclass {
public static void main(String[] args) {
Double fnum, snum;
Double ans = 0.0;
String opr;
Scanner getIn = new Scanner(System.in);
System.out.print("Enter your first number: ");
fnum = getIn.nextDouble();
System.out.print("Enter your second number: ");
snum = getIn.nextDouble();
System.out.print("Enter the operation: ");
opr = getIn.next();
if(opr == "add") {
ans = fnum + snum;
}
System.out.print("Answer is " + ans);
}
}
...replace if(opr == "add") with if("add".equals(opr)), you want to check that the string are equals not that they are the same object!
Related
This question already has answers here:
Convert a number to 2 decimal places in Java
(4 answers)
Closed 4 months ago.
I was given a task to calculate the average mark of unknown number of tests in JAVA, I was able to solve the problem but not thoroughly because my output has more than 2 decimal point. May I please be helped to only show my output rounded off to two decimal places. (I am a first year students and I don't know much in JAVA). Here's a snippet of my code.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class number2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number_Of_Tests = 0;
int total_marks = 0;
int mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
while (mark >= 0) {
number_Of_Tests = number_Of_Tests + 1;
total_marks = total_marks + mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
}
float average = total_marks / (float) number_Of_Tests;
JOptionPane.showMessageDialog(null, "The average is: " + average, "AVERAGE",1);
scan.close();
}
}
let's say my input is: 25 , 26 and 28 then 0, my output should be 26.34 but instead I get 26.333334
import java.util.Scanner;
import javax.swing.JOptionPane;
public class number2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number_Of_Tests = 0;
int total_marks = 0;
int mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
while (mark >= 0) {
number_Of_Tests = number_Of_Tests + 1;
total_marks = total_marks + mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
}
float average = total_marks / (float) number_Of_Tests;
JOptionPane.showMessageDialog(null, "The average is: " +"% .2f" + average, "AVERAGE",1);
scan.close();
}
}
The first example is a formatter that always produces two decimal places. The second one only produces as many decimal places (up to 2) as needed. Usage: DF_TWOPLACES.format(average);
final private static DecimalFormat DF_TWOPLACES = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US));
final private static DecimalFormat DF_TWOPLACES = new DecimalFormat("0.##", DecimalFormatSymbols.getInstance(Locale.US));
This question already has answers here:
Java User Input and Difference between readInt and nextInt?
(2 answers)
Closed 3 years ago.
I am trying to make a simple adding machine.
public class AddingMachine { // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();
String name = readLine("What is your name? ");
int num1 = readInt("What is the first number? ");
int num2 = readInt("What is the second number? ");
System.out.println();
int sum = num1 + num2;
System.out.print(name);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}
The error message reports "cannot find symbol" readInt or readString but I have found the methods on the Oracle website:
https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
There may be some other methods that can solve this problem, but I think the read methods make for a very simple input/output example.
This is the solution,
readInt and readLine are the methods from scanner class.
public class AddingMachine { // Save as 'AddingMachine.java"
public static void main(String[] args) {
System.out.println();
System.out.println("Welcome to the Adding Machine.");
System.out.println();
System.out.println("What is the first number?");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
System.out.println("What is the second number?");
int num2 = sc.nextInt();
System.out.println();
int sum = num1 + num2;
System.out.print(sum);
System.out.print(", the sum is: ");
System.out.print(sum);
}
}
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I am trying to find the average number of students per class but when I test my program, it only prints 1 or 0 everytime. Any help would be appreciated.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Please enter the number of Students: ");
int s = reader.nextInt();
System.out.println("Please enter the number of classes: ");
int c = reader.nextInt();
int numbStud1 = 0;
int numbClass1 = 0;
double averages =calcAverage (numbStud1 + c,numbClass1 + s) ;
System.out.println("The average is: " + averages);
}
public static double calcAverage(int numbStud, int numbClass){
double average1;
average1 = numbStud / numbClass;
return average1;
}
}
The issue is this:
double averages =calcAverage (numbStud1 + c,numbClass1 + s) ;
Replace it with exchanging c with s:
double averages =calcAverage (numbStud1 + s,numbClass1 + c) ;
And if you want avrage more precision use :
average1 = (numbStud*1.0 / numbClass*1.0);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
so I'm trying to ask the user to input value for control right after the SOP is done. But it skips it for some reason.
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What's your balance?");
double initialBalance = keyboard.nextDouble();
Account chase = new Account(initialBalance);
System.out.println(chase + "; Would you like to deposit or withdraw?");
String control = keyboard.nextLine();
if(control == "deposit")
{
double deposit = keyboard.nextDouble();
System.out.println("How much would you like to deposit? " +
deposit);
chase.deposit(deposit);
System.out.println(chase);
}
}
Here is a snippet of your code with the changes required.
Scanner keyboard = new Scanner(System.in);
System.out.println("What's your balance?");
double initialBalance = keyboard.nextDouble();
keyboard.nextLine();
Account chase = new Account(initialBalance);
System.out.println("; Would you like to deposit or withdraw?");
String control = keyboard.nextLine();
if (control .equals("deposit") ){
System.out.println("How much would you like to deposit? " );
double deposit = keyboard.nextDouble();
keyboard.nextLine();
The comments are clear. You would need to see the two post that they pointed to. Briefly there is a line separator that comes in and the importance of the equals method and its significance.
New Code:
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What's your balance?");
double initialBalance = keyboard.nextDouble();
Account chase = new Account(initialBalance);
System.out.println(chase + "; Would you like to deposit or withdraw?");
String control2 = "deposit";
boolean control = keyboard.next().equalsIgnoreCase(control2);
if(control == true)
{
double deposit = keyboard.nextDouble();
System.out.println("How much would you like to deposit? " +
deposit);
chase.deposit(deposit);
System.out.println(chase);
}
}
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
OK, So I have this code:
import java.util.Scanner;
public class Library
{
int Acc_Num;
String Title;
String Author;
public void input()
{
Scanner s = new Scanner (System.in);
System.out.println("Enter Your Accession Number");
Acc_Num = s.nextInt();
System.out.println("Enter The Title Of The Book");
Title = s.nextLine();
System.out.println("Enter The Author Of The Book");
Author = s.next();
}
public void compute()
{
Scanner s = new Scanner (System.in);
System.out.println("Enter Number Of Days Late");
int Day_Late = s.nextInt();
int Fine = 2 * Day_Late;
System.out.println("Fine: " + Fine);
}
public void display()
{
System.out.println("Accession Number\t\tTitle\t\tAuthor");
System.out.println(Acc_Num + "\t\t" + Title + "\t\t" + Author);
}
public static void main(String[] args)
{
Library Mem1 = new Library();
Mem1.input();
Mem1.compute();
Mem1.display();
}
}
The thing is, when it goes into the function to get input, it take input for Title but not for author. Have attached a screenshot of the program running in cmd.
Would love to have this solved.
Is this a Scanner glitch? Or Am I doing something wrong?