Problems with array. Java [closed] - java

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 4 years ago.
Improve this question
I started learning java yesterday and this is the first time i'm using array. This is the code
import java.util.Scanner;
public class array
{
public static void main(String[] args)
{
int num[];
num = new int[5];
Scanner input = new Scanner(System.in);
int i;
System.out.println("Insert 5 numbers:");
for(i = 0; i < 5; i = i + 1);
{
System.out.print("Insert the " + i + "° number: ");
num[i] = input.nextInt();
}
System.out.print("The numbers you entered are: ");
for(i = 0; i < 5 ; i = i + 1)
{
System.out.println(num[i] + " ");
}
}
}
When i try to run it i get this problem:
Insert 5 numbers:
Insert the 5° number: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at array.main(array.java:14)

Two things.
You have a semicolon at the end of your loop. That will cause the loop to run until i = 5 and you're kind of stuck with that value now. Remove it.
i will remain 5 after the first loop for the same reason as above. Declare and initialize i inside of your for statement.
for(int i = 0; i < 5; i = i + 1) {
// the rest of your block
}

Related

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);

Java comparing 10<-5 gets true [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 4 years ago.
Improve this question
i'm learning java and got a problem, I have an max/min exercise and tried the code bellow, for positive numbers it work fine, but when i try negative numbers the things got messy.
If you enter 5 you get min = 5 and max = 5, if you enter 10 after, you get min = 5 max = 10, so far so good, but if you type -5, you get min = -5 and max = -5, and things still get weirder, if you type 7, you get min =-5 and max = 7.
Can someone explain why this happens?
public static void main(String[] args) {
int ctrl;
int min = 0;
int max = 0;
boolean hasNextInt;
Scanner scanner = new Scanner(System.in);
int counter = 0;
while (true) {
System.out.println("Enter your number:");
hasNextInt = scanner.hasNextInt();
if(hasNextInt) {
ctrl = scanner.nextInt();
if(counter == 0) {
min = ctrl;
max = ctrl;
counter ++;
}
if(min>ctrl)
min = ctrl;
if(max<ctrl);
max = ctrl;
System.out.println("Minimum Number entered: " + min);
System.out.println("Maximum Number entered: " + max);
System.out.println("");
scanner.nextLine();
}else {
System.out.println("Invalid Number. Program stop working.");
break;
}
}
scanner.close();
}
if(max<ctrl);
max = ctrl;
That indentation is highly misleading.
Use an IDE that does code formatting. What you really wrote is
if(max<ctrl){}
max = ctrl;
Also avoid if/else/for etc without {}.

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);

Java 8 Unreachable Code [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 8 years ago.
Improve this question
I am fairly new to Java and I ran into this problem once or twice in one of my books. This is a super simple program and I just don't understand why it's not working. I know when you use return, anything after it in the method is meaningless. So does this mean after you perform an for statement or an if statement that is a return?
I am using Java 8 on Windows 8 in the latest version of Eclipse.
This is my simple program:
// Find the sum of 1 through 50 and the average.
class SumAndAverage
{
public static void main(String args[])
{
int sum = 0;
double average = 0;
for(int i = 1; 1 <= 50; i++)
{
sum += i;
}
// the following code is "unreachable"
average = sum / 100;
System.out.println("The sum is: " + sum);
System.out.println("The average is " + average);
}
}
1 is always less than or equal to 50, isn't it? You probably meant to compare i with 50:
for(int i = 1; i <= 50; i++)
{
sum += i;
}
for(int i = 1; 1 <= 50; i++)
1 is always less than or equal to 50.

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