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);
}
}
Related
i still new at coding, and i am wonder if anyone could help me with java code.
additionally, explaining how to deal with this issue.
here is my code below:
import java.util.Scanner;
public class SumAverage {
public static void main(String[] args) {
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
int number1, number2, sum;
System.out.print("Enter first integer: "); // prompt
number1 = input.nextInt(); // read 1st number from user
System.out.print("Enter second integer: "); // prompt
number2 = input.nextInt(); // read 2nd number from user
sum = number1 + number2;
double average = (double) sum / 2;
String message = "Sum is";
System.out.printf("%s %d\n", message, sum);
System.out.printf("Average is %.2f\n", average);
} // end main method
} // end class SumAverage
I'm trying to store the sum of 2 numbers inside a while loop so that once the loop ends multiple sums can be added up and given as a total sum, however I am rather new to Java and am not sure how to go about doing this.
I'm trying to use an array but I'm not sure if it is the correct thing to use. Any help would be greatly appreciated.
import java.util.Scanner;
public class StoredWhile{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int TotalNum[]=new int[10];
Int Num1, Num2, AddedNum;
String answer;
do{
System.out.println("Please enter a number");
Num1 = input.nextInt();
System.out.println("Please enter a second number");
Num2 = input.nextInt();
AddedNum = Num1 + Num2;
System.out.println("The sum of the two entered numbers is " + AddedNum);
TotalNum[0]=AddedNum;
TotalNum[1]=;
System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
answer = input.next();
}
while (answer.equals("y"));
System.out.println("The total sum of all the numbers you entered is " + TotalNum);
}
}
There is a data container called ArrayList<>. It is dynamic and you can add as many sums as you need.
Your example could be implemented like this:
public class StoredWhile{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> listOfSums = new ArrayList<>();
int Num1, Num2, AddedNum;
String answer;
do{
System.out.println("Please enter a number");
Num1 = input.nextInt();
System.out.println("Please enter a second number");
Num2 = input.nextInt();
AddedNum = Num1 + Num2;
System.out.println("The sum of the two entered numbers is " + AddedNum);
listOfSums.add(AddedNum);
System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
answer = input.next();
}
while (answer.equals("y"));
// Then you have to calculate the total sum at the end
int totalSum = 0;
for (int i = 0; i < listOfSums.size(); i++)
{
totalSum = totalSum + listOfSums.get(0);
}
System.out.println("The total sum of all the numbers you entered is " + totalSum);
}
}
From what I see, you come from a background of C# (Since I see capital letter naming on all variables). Try to follow the java standards with naming and all, it will help you integrate into the community and make your code more comprehensible for Java devs.
There are several ways to implement what you want, I tried to explain the easiest.
To learn more about ArrayList check this small tutorial.
Good luck!
Solution with array:
import java.util.Scanner;
public class StoredWhile{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int TotalNum[]=new int[10];
int Num1, Num2, AddedNum;
String answer;
int count = 0;
do{
System.out.println("Please enter a number");
Num1 = input.nextInt();
System.out.println("Please enter a second number");
Num2 = input.nextInt();
AddedNum = Num1 + Num2;
System.out.println("The sum of the two entered numbers is " + AddedNum);
TotalNum[count]=AddedNum;
count++;
System.out.println("Would you like to calculate the sum of two more numbers (y/n)?");
answer = input.next();
}
while (answer.equals("y"));
int TotalSum = 0;
for(int i = 0; i <count; i++ ) {
TotalSum += TotalNum[i];
}
System.out.println("The total sum of all the numbers you entered is " + TotalSum);
}
}
This solution is not dynamic. There is risk that length of array defined on beginning will not be enough large.
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I am learning about Arrays and had gotten this assignment. Whenever I run the code, the value which is supposed to go into String array goes into Int. It skips over the name[i]=sc.nextLine(); completely.
Any Help will be appreciated.
import java.util.*;
public class Marks_Remarks {
public static void main(String []Args){
Scanner sc= new Scanner (System.in);
System.out.println("Enter how many students");
int n= sc.nextInt();
String name[]= new String[n];
int roll[]= new int[n];
int sub1[]= new int[n];
int sub2[]= new int[n];
int sub3[]= new int[n];
for (int i=0;i<n;i++){
System.out.println("Enter Name");
name[i]= sc.nextLine();
System.out.println("Enter Roll No.");
roll[i]= sc.nextInt();
System.out.println("Enter marks in Three Subjects");
sub1[i]= sc.nextInt();
sub2[i]= sc.nextInt();
sub3[i]= sc.nextInt();
}
int avg; String remark;
for (int k=0;k<n;k++){
avg= (sub1[k]+ sub2[k]+ sub3[k])/3;
if (avg>=85)
remark= "Excellent";
else if(avg>=75 && avg<=84)
remark= "Distinction";
else if(avg>=60 && avg<=74)
remark= "First Class";
else if(avg>=40 && avg<=59)
remark="Pass";
else
remark="Poor";
System.out.println("Name: "+ name[k]+ "\tRoll No.: "+ roll[k]+ " \tAverage: "+ avg+ " \tRemark: " + remark);
avg=0;
}
}
}
Use name[i]=sc.next(); instead of : name[i]=sc.nextLine();
It may help you.
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!
import java.util.*;
public class Group2_p2 {
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num1 = 0, num2, answer = 0;
System.out.println("Please enter first number");
num1 = console.nextInt();
System.out.println("Please enter second number");
for (num2=console.nextInt() ; num2>0; num2--) {
answer = num1 * num2;
}
System.out.print(answer);
}
}
Please enter first number
[5]
Please enter second number
[2]
5>
My issue is that I'm not receiving the correct answer. I have to take two numbers and the first number is to be raised to the power of the second number.
Most likely a really silly mistake but I'm having a brain fart on this. I'm still a beginner.
Try this:
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int num1 = 0, num2, answer = 1;
System.out.println("Please enter first number");
num1 = console.nextInt();
System.out.println("Please enter second number");
for (num2=console.nextInt(); num2 > 0; num2--) {
answer = answer * num1;
}
System.out.print(answer);
}
You should keep multiplying your answer by num1 every iteration and updating answer. Also you should change answer to be 1 to start with.