Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
3.2: Sum of Cubes
Write an application that prints the sum of cubes. Prompt for and read two integer values and print the sum of each value raised to the third power.
SPECIFICATION OF PROMPTS, LABELS AND OUTPUT :Your code should use these prompts: "integer1: " and "integer2 ".The prompts should not force the user to type the required input on the next line. After all the inputs are read, the output should consist of a single line consisting of the label "the sum of the cubes of these numbers is:" followed by your calculated value . For example:
integer1: 3
integer2: 5
the sum of the cubes of these numbers is: 152
SPECIFICATION OF NAMES: Your application class should be called CubeSum
Error:
Expected Output:
integer1:·↵
integer2:·↵
the·sum·of·the·cubes·of·these·numbers·is:·9
Actual Output:
integer1:integer2:the·sum·of·these·cubes·is:·9↵
My Code:
import java.util.*;
public class CubeSum {
public static void main (String args []) {
Scanner scan = new Scanner(System.in);
int integer1, integer2, cube1, cube2;
System.out.print("integer1: ");
integer1=scan.nextInt();
cube1 = (int)Math.pow(integer1 ,3);
System.out.print("integer2: ");
integer2=scan.nextInt();
cube2 = (int)Math.pow(integer2 ,3);
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
}
}
Change this line:
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
You was missing the + to concat the output.
Also, math is a class, so you should use it like this:
cube1 = (int) Math.pow(integer1, 3);
EDIT
import java.util.*;
public class CubeSum {
public static void main (String args []) {
Scanner scan = new Scanner(System.in);
int integer1, integer2, cube1, cube2;
System.out.print("integer1: ");
integer1=scan.nextInt();
cube1 = (int)Math.pow(integer1 ,3);
System.out.print("integer2: ");
integer2=scan.nextInt();
cube2 = (int)Math.pow(integer2 ,3);
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
}
}
In your line that prints the output, you are indeed missing a '+':
Change the line to:
System.out.println("the sum of these cubes is: " + (cube1 + cube2));
Additionally, there's two more mistakes:
cube1 = math.pow(int1 ,3);
should be
cube1 = (int)Math.pow(integer1 ,3);
and
cub2 = Math.pow(int2, 3);
should be
cube2 = (int)Math.pow(integer2, 3);
since you never actually define 'int1' or 'int2'
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
First day learning Java and I came across this problem: I need to divide 2 floats(given from command line input). I am getting an error every time I input my 2 floats. I included my code down below
import java.util.Scanner;
public class doubledivision
{
public static void main(String [] args)
{
float num1, num2;
Scanner in = new Scanner(System.in);
System.out.print("Please enter two floating point numbers: ");
num1 = in.nextInt();
num2 = in.nextInt();
float result = num1 / num2;
System.out.println(result);
}
}
If you want to read a number of type float from the user, you need to use :
in.nextFloat()
because this in.nextInt() will return a value of type int.
Also you need to consider some things :
You need to clear the scanner since you want 2 floating numbers , why ? because if the user enters :
>>> 1.0 2.0
The space will make your scanner assigne num1 with 1.0 and num2 with 2.0.
So if you want to ask the user 2 times use :
num1 = in.nextFloat();
in.nextLine(); // to refresh
num2 = in.nextFloat();
If the user enters zero (any number) divided by 0 will blow up your app , so add a check :
if(num2==0){
System.out.println("NaN");
}
else {
float result = num1 / num2;
System.out.println(result);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to pass user's input to the array through a scanner. The goal is to make the average value of the inputs, this is the code:
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
double[] str1 = new int [20] ;
str1 = scanner.next.Double();
System.out.println("Average is: " + Arrays.stream(str1).summaryStatistics().getAverage());
}
}
Below 2 statements are having syntax error and because of this code will not compile:
double[] str1 = new int [20] ;
str1 = scanner.next.Double();
you need to declare double array as double[] str1 = new double[20];
you need to take the double value as input like this scanner.nextDouble(); and as str1 is array we can't store the input directly as str1 = scanner.nextDouble(); in that case your program will give compile time error
Type mismatch: cannot convert from double to double[]
To resolve this error we need to store the value of every input at specific index like str1[0] = scanner.nextDouble();
Below is the working code as per your requirement(as I understand from your question):
Scanner scanner = new Scanner(System.in);
double[] str1 = new double[20] ;
for(int i = 0; i < 20;i++) {
str1[i] = scanner.nextDouble();
}
System.out.println("Average is: " + Arrays.stream(str1).summaryStatistics().getAverage());
I hope it will help you resolve the error you're facing.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import java.util.Scanner;
public class FantahunKeburAlmostIsoscelesRightTriangle{
public static void main (String []args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the longest side: ");
double longest = s.nextDouble();
System.out.println("Enter 2nd side: ");
double second = s.nextDouble();
System.out.println("Enter 3rd side: ");
double third = s.nextDouble();
double longsq = Math.pow(longest, 2);
double secondsq = Math.pow(second, 2);
double thirdsq = Math.pow(third, 2);
double secthirdsq = secondsq + thirdsq;
double SecLongLength = second + 1 ;
if (secthirdsq==longsq) {
System.out.println("This triangle is an almost-Isosceles right triangle. ");
}
else if (secthirdsq!=longsq) {
System.out.println("This triangle is not a right triangle. ");
else (SecLongLength!=longest) {
System.out.println("This triangle is a right triangle, but not almost-Isosceles.");
}
}
}
}
I get these errors after I try to run the code. Basically, if I input numbers like 5, 4, and 3 for longest, second, and third respectively I should get "this triangle is an almost-Isosceles right triangle" as an "Almost Isosceles Right Triangle" is a right triangle where the numbers are one integer from each other, and this function works. When I input numbers like 10, 8, and 6 I should get this output: "This triangle is a right triangle, but not almost-Isosceles." But alas, I get the errors below. I tried to make it an else if and it then gave me a ";" error which corrected and more errors came up. What can I do to get the output I want?
FantahunKeburAlmostIsoscelesRightTriangle.java:33: error: 'else' without 'if'
else (SecLongLength!=longest) {
^
FantahunKeburAlmostIsoscelesRightTriangle.java:33: error: not a statement
else (SecLongLength!=longest) {
^
FantahunKeburAlmostIsoscelesRightTriangle.java:33: error: ';' expected
else (SecLongLength!=longest) {
Take a look at your code, This is wrong
else if (secthirdsq!=longsq) {
System.out.println("This .......");
else(SecLongLength!=longest) { // this else inside if without a if
System.out.println("This .....");
}
}
There must be a if to have else. It is obvious what is the point of having else without a if?
You can follow following order
if(condition1){
else if(condition2){
}else{
}
Two things are wrong...
1. the closing bracket
2. the else branch.. either else without or else-if with condition
else if (secthirdsq!=longsq) {
...
else (SecLongLength!=longest) {
...
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
"Syntax error on token ";", Primitive Type expected after this token" is the message i get rite after the semi colon on this statement "String data = input.next(); " and i don't know why.
import java.util.*;
public class PorterHouse {
public static void main(String[] args){
System.out.println("enter interest rate, loan mount, and number of years: ex. "+
" 7.9-400000-5");
Scanner input = new Scanner(System.in);
String data = input.next();
[] b = data.split("[-]");
double interstRate = Double.parseDouble(b[0]);
double loanPrincipal = Double.parseDouble(b[1]);
double loanLength = Double.parseDouble(b[2]);
double monthlyPayment = (b[1]*b[2] / 1);
double totalPayment = ( monthlyPayment * loanLength * 12);
System.out.println("Monthly Pament: " + monthlyPayment);
System.out.println("totalPayment: " + totalPayment);
}
}
Try to change this:
[] b = data.split("[-]");
with this:
string[] b = data.split("[-]");
Presently [] b has no data type. So do provide a data type to it.
I guess instead of :
[] b = data.split("[-]");
you need something like :
String[] b = data.split("[-]");
also
you might wanna replace :
double monthlyPayment = (b[1]*b[2] / 1);
by :
double monthlyPayment = (loanPrincipal * loanLength / 1); //idk why divide by 1??
cuz, b[1] and b[2] are two Strings.