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.
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 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'
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.
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
I am a beginner,I'm having problem with my very first code.I just trying to solve coupling shaft alignment.
It says, "illegal start of expression".Can someone help me with this problem? thanks
Here's my code:
import java.util.Scanner;
public class Front {
public static void main(String []args){
Scanner input = new Scanner(System.in);
Double A = input.nextDouble();
Double B = input.nextDouble();
Double C = input.nextDouble();
Double S = input.nextDouble();
Double M = input.nextDouble();
Double F = B / A * (S-M)/2 - S/2;
System.out.println("The front foot is: " + F +);
}
}
You missed an operand after your last + in System.out.println("The front foot is: " + F +);
Another option is to use formatted output:
System.out.printf("The front foot is: %f\n", F.floatValue());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to fill an array of integers in Java by asking the user to enter the number once ?
for example :
Enter 3 integers >: 123
then I want to get the array filled like that :
arr[0]=1
arr[1]=2
arr[2]=3
I hope the question is clear :)
rgds
One way of going about this would be to take the integer in as a String. You can then use toCharArray() in order to grab an array of the characters - these characters can then be individually parsed into Integer by utilizing Character.getNumericValue().
See the String and Character APIs for more details on those methods.
Small example of what I mean:
String numbers = "123";
char[] numArray = numbers.toCharArray();
for (char c : numArray) {
int num = Character.getNumericValue(c);
// Do what you will with the number.
}
As noted, this particular implementation would only work in this format if you are assuming that each integer is single digit.
How about this:-
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < array.length; i++ ) {
array[i] = input.nextInt();
}