I am getting a error:cannot find symbol when compiling [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am trying to calculate and round up with an IF statement, the code is below. The error comes on the line: if (partterms < 6)
// Setup Array List for Course CU
ArrayList<Double> coursecu = new ArrayList<Double>();
// Read inputs for Course Units and Sum the Value
System.out.println("Please enter Individual Course CU values, Q to quit:");
Scanner in2 = new Scanner(System.in);
while (in.hasNextDouble())
{
coursecu.add(in.nextDouble());
}
double sum = 0;
for(int i = 0; i < coursecu.size(); i++)
{
sum = sum + coursecu.get(i);
}
System.out.println();
System.out.println("Total Credit Units Required for Graduation");
System.out.println(sum);
// Calculate the Number of Terms to Completion
{
double fullterm = sum / planned_units; // Sets Whole Terms
double partterm = sum % planned_units; // Sets Partial Terms
}
if (partterm < 6)
{
number_terms = fullterms++;
}
else
{
number_terms = fullterms;
}

{ ←
double fullterm = sum / planned_units; // Sets Whole Terms
double partterm = sum % planned_units; // Sets Partial Terms
} ←
Remove { and } and the variables will be known outside this weird block.

Related

Need to print number of elements, total and average for random number array java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
package genrandom;
import java.util.Random;
public class GenRandom {
static int maxNumber = 0;
public static int GenRandom() {
return new Random().nextInt(899)+100;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int randomNumber = GenRandom();
if (randomNumber > maxNumber) {
maxNumber = randomNumber;
}
System.out.println(randomNumber);
}
System.out.println("The greatest file size is: " + maxNumber + "MB");
} // TODO code application logic here
}
I am able to print the greatest number however need to print how many elements there are, the total and the average
For reference, this can be solved easily by using streams (Java 8):
var stats = IntStream.generate(GenRandom::GenRandom)
.limit(10)
.summaryStatistics();
and then stats.getMax(), stats.getSum() and stats.getAverage(). See the documentation for more details.
It is also efficient as all of them are computed in one go instead of multiple iterations. And you even get multithreading for free by just calling parallel(), in case you have thousands of numbers to deal with.
This calculates the average and total by keeping track of each in the loop:
public static void main(String[] args) {
int numbers = 0; // How many random numbers have been generated
double total = 0; // Total of all random numbers
for (int i = 0; i < 10; i++) {
int randomNumber = GenRandom();
if (randomNumber > maxNumber) {
maxNumber = randomNumber;
}
numbers++; // Add 1 to count of random numbers generated
total += randomNumber; // Add number to total
System.out.println(randomNumber);
}
System.out.println("The greatest file size is: " + maxNumber + "MB");
System.out.println("There were " + total + " total numbers");
// Average is the total divided by the amount of numbers
System.out.println("The average file size was: " + (total/numbers) + " MB");
}

How to link one with another parts [closed]

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 3 years ago.
Improve this question
I have this homework. I'm having a hard time linking the first part with the second. This is what I have. I know I'm missing something at some point to indicate that from the number input it should add the next 100 numbers.
package test;
import java.util.Scanner;
public class Ex216 {
public static void main(String[] args) {
// Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen
Scanner myInput = new Scanner(System.in);
int =a
int sum;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
for (int n = a; n <= 100; n++)
System.out.printf("Sum = %d\n", sum);
}
}
This is what is casing me the trouble.
first of all,int =a is not a valid expression. it should be int a; then as you want to add next 100 number from the given value, you need to add those values into sum, such as sum = sum+number.
Here is a code snippet:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);

Divide an int into whole numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for a way to divide an int into whole numbers.
What I mean by this: if I have a number 30 and I want to divide this by 4, I want the output to be 8,8,7,7.
Is there a way in Java to do this?
Thanks in advance.
Sure, Java is turing complete and therefore allows you to implement any algorithm.
I assume that the difference between the resulting numbers should be at most one - you did not explicitly write this.
Try this:
final int input = 30;
final int numberOfPieces = 4;
final int quotient = input / numberOfPieces;
final int remainder = input % numberOfPieces;
int [] results = new int[numberOfPieces];
for( int i = 0; i < numberOfPieces; i++ ) {
results[i] = i < remainder ? quotient + 1 : quotient;
}
This code first calculates the integer quotient and then equally distributes the remainder to the first "pieces".
Since you don't want equal splits of the number, what you may do is :
Divide the number by how many ever parts you want.
Round() the result
Add up the rounded of number how many ever times required & check if sum is same, if not add or subtract 1 as necessary.
Eg: N = 150 , parts = 4
=> 37.5 , Round it round(37.5) => 38
Now, 38*4 = 152 and 152-150 = 2 so subtract 2 from a number and your answer is 38, 38, 38 & 36.
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
float number = 30.0f;
float parts = 4.0f;
float val = number / parts;
val = Math.round(val);
if (val * parts == number){
System.out.println("Numbers are:");
for (int i = 0; i < parts; i++)
System.out.println(val);
}
else {
int diff = Math.round((val * parts) - number);
System.out.println("Numbers are:");
for(int i = 0;i < parts - 1; i++)
System.out.println(val);
System.out.println(val - diff);
}
}
}
Output:
Numbers are:
8.0
8.0
8.0
6.0
If you want to equally share the difference in the above case then just replace the else part with this:
else {
int diff = Math.round((val * parts) - number);
System.out.println("Numbers are:");
for (int i = 0; i < parts - diff; i++)
System.out.println(val);
for (int i = 0; i < diff; i++)
System.out.println(val - 1);
}
Your output will be:
Numbers are:
8.0
8.0
7.0
7.0

How to take the sum of squares in java? [closed]

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 7 years ago.
Improve this question
So I am a beginner in java and am just learning the loop concept. One of my assignments is to modify this code below so that it prints out the sum of the squares of the values, rather than just the sum of the values:
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i);
}
System.out.print("Sum from "+ first +" to " + second );
System.out.println(" is " + sum);`
Apparently I only have to modify one line of this code. I tried:
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i^2);
}
where I thought if you just added the i^2 value inside of the parenthesis it would work, but it did not. Any help on how I do this/any help on understanding how a loop works would be greatly appreciated.
As the comments said,
change i^2 to i*i
Scanner scan = new Scanner(System.in);
System.out.println("Enter two numbers");
System.out.println("Separate with spaces");
System.out.println("Code sums from first to second");
int first = scan.nextInt();
int second = scan.nextInt();
int sum = 0;
for(int i = first; i <= second; i++)
{
sum += (i*i);
}
I applied the formula of sum of squares of n terms here and subtracting first one I had used n*(n+1)*(2*n+1)/6 formula here with out no loop performance o(1)
sum = (second*(second+1)*((2*second)+1))/6 - (first*(first+1)*((2*first)+1))/6;
Just change
sum += (i);
to
sum += (i*i);

Program where user enters 2 inputs and a limit. It finds the sum of the multiples. please check [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The user must enter x, y, and the limit. The program must find the multiples of these numbers that are below the limit that the user set themselves. The program adds up all the multiples and prints just that number at the end, and not all the multiples. For some reason it just isn't working for me and I can't figure it out.
import java.util.Scanner;
public class SumMultiples.java {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter x value: ");
int x = scanner.nextInt();
System.out.print("Enter y value: ");
int y = scanner.nextInt();
System.out.print("Enter limit value: ");
int limit = scanner.nextInt()
int sum = 0;
for (int i = 0; i < limit; i++) {
if (((i % x) == 0 || ((i % y) == 0))) {
sum += i;
}
}
}
System.out.println(sum);
}
From copying your code into Eclipse:
Firstly you are missing a ; when you are getting the input for limit.
Secondly your System.out.println(sum); is outside of your main method, it should be at the end of the method i.e. after your for loop.
Thirdly, you have named your class SumMultiples.java. This is not a valid name, rename it to SumMultiples.
You should be getting compile errors and your IDE should help you work this out. If you aren't using an IDE, please do so as it will help you debug these issues and help you get familiar with Java.

Categories