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 5 years ago.
Improve this question
This question is pretty specific for my problem, that is why I am creating a new question. The second method in this program is supposed to make a row of the number 1 2 3 4 5 6 7 8 9 10. The only problem I am having is that I don't know how to print this out in the main method.
public class Uppgift1_6a
{
public static void main(String[] args)
{
for(int k = 0; k < 10; k++)
{
int tal = Numberline(k);
System.out.print(tal);
}
}
public static int Numberline(int tal1)
{
int tal = 1;
for(int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
}
Right now it prints out all the number from 11 to 19. And if I change it, it only prints out either 10 or 11.
Look closely at the code:
public static int Numberline(int tal1)
{
int tal = 1;
for (int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
The for loop literally does absolutely nothing - you're only returning the final result. The final result is always exactly equal to tal1 + 10; again, what the for loop did up this point makes no difference. (I'd encourage you to step through the code with a debugger to convince yourself of that fact).
If you want it to print out the values as you're going through the for loop, you need to do something like:
for (int i = 1; i < 11; i++)
{
// You may need to modify this line too, depending on what values you want printed
tal = tal1 + i;
// Print the value here
System.out.print(tal);
}
because the way you've written it it'll only print out the final value of tal (the one you returned).
Related
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 3 years ago.
Improve this question
I was trying to obtain the minimun value and its index between an array of int.
I can't understand why if I use the for-loop inside the main method it doesn't work, but if I use the same code in an aux method it works. The code should be right.
The part of the code that is commented is the for-loop that doesn't work.
package minimoArray;
import java.util.Scanner;
public class minimoArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Inserisci 10 numeri interi: ");
int [] Arr = new int [10];
//int a = Arr[0];
int b = 0;
for (int i = 0; i < Arr.length; i++) { //NON si può riempire l'array con for-each
Arr[i] = scanner.nextInt();
}
/*
for (int i = 0; i < Arr.length; i++) {
if (Arr[i] < a) {
a = Arr[i];
b = i;
}
}*/
int minimo = minimo(Arr);
for (int i = 0; i < Arr.length; i++) {
if (Arr[i] == minimo) {
b = i;
}
}
System.out.println(" il minimo è: " + minimo);
System.out.println(" l'indice del minimo è: " + b);
}
private static int minimo (int [] a) {
var min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min ) {
min = a[i];
}
}
return min;
}
}
Edit: Just saw that the a is commented out too, my mistake. However, here Nicktar's answer applies with the reason why, you set a to the first element of the array, whose numbers weren't even set yet. The solution stays the same though.
First of all, stick to the naming convention of Java. Classes start with an uppercase letter, variables start with a lowercase letter.
Your mistake in the for loop within your main method is that a isn't even definied there, therefore the whole loop shouldn't even compile.
Simply add the var a = Arr[0]; before the loop and start the loop at the index 1.
The initialization of a in your main method is to early. You set a to Arr[0] before the array is written which sets it to 0 because primitive int is initalized to 0. So your code in the main methods compares all your array to 0.... This would find a minimum that is negative only.
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 6 years ago.
Improve this question
public static int keywordsChecker(String essay,String key) {
int count = 1;
String[] k=key.split(",");
for (int i = 0; i < k.length-1; i++) {
if (essay.contains(k[i])) {
count++;
}
}
return count;
}
To take into account that each keyword searched for may occur more than once, and to count such occurrences, you may use this inside your for loop:
int indexOfOccurrence = essay.indexOf(k[i]);
while (indexOfOccurrence > -1) {
count++;
indexOfOccurrence = essay.indexOf(k[i], indexOfOccurrence + 1);
}
There are a couple of other issues in your code: I believe you need to initialize count to 0 (not 1). And to count also the last keyword in key your for loop should be for (int i = 0; i < k.length; i++) (without subtracting 1 from k.length). If you want, using <= would also work: for (int i = 0; i <= k.length-1; i++), but this is non-standard, so I would not recommend it.
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 6 years ago.
Improve this question
I am having a global variable and I want to concatenate some value to this variable in a for loop and want the value outside of a for loop.
But the problem is whenever the for loop starts it's next iteration value of variable is lost.
my code
function hello() {
StringBuffer Id = new StringBuffer(20);
Id.append("");
for (i = 1; i < 10; i++) {
Id.append(i);
}
System.out.println(Id);
}
You need System.out.println(id); based on your comment that has your code.
You just need to return id from your method to caller to hold the value.
String[][] matrix = { {"1", "2", "3"} };
String[] y = {"TEST" ,"BUG"};
int a = 0;
int value = 0;
for (int i = 0; i < y; i++)
{
for (int j = 1; j < 4; j++)
{
value = Integer.parseInt(matrix[i][j - 1]);
System.out.println(value); //this is OK it print me 3 values
}
}
System.out.println(value);
Declaring variables inside or outside of a loop
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
I try to print the odd numbers in Java that are inside the array but this algorithm doesn't work ... May someone help me ?
The printing result is that :
"Exception in thread "main" .java.lang.ArrayIndexOutOfBoundsException: 7
at JavaArray.main(JavaArray.java:12)"
Code :
public class JavaArray {
public static void main(String[] args) {
int[] myArray = {1,3,4,5,8,9,10};
int i = 0;
for(i = 0; i < myArray.length; i++); {
if(myArray[i] % 2 == 1) {
System.out.println(myArray[i]);
}
}
}
}
Remove the semi-colon that is terminating your for loop
for (i = 0; i < myArray.length; i++);
^
Because you have placed semicolon after for loop, variable i increments till length of array(here 7). After that loop ends and you are trying to access myarray element through i which is 7 so it is giving out of bound exception.
Besides the extra ; you need to remove, you can consolidate by declaring the int in the loop declaration:
for (int i = 0; i < myArray.length; i++) {
.
.
.
}
Beside #Reimus point , you can also do it like below , sort the array if it's not sorted yet, in your case it is sorted . FYI, Instead of Collections.sort which is above O(N) complexity use a Hash Set.
public static void main(String[] args) {
int[] myArray={1,3,4,5,8,9,10};
Arrays.sort(str);
for (int i = 1; i < myArray.length; i++) {
if (str[i] == str[i - 1]) {
System.out.println("Dupe-num: " + str[i];
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
import java.util.*;
import javax.swing.*;
public class Practice {
public static void main(String[] args){
int[] numbers = {1, 2, 3, 14, 15, 16, 17};
getSmall(numbers);
}
public static void getSmall(int[] ar){
int small=0;
for(int i=0; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
}
The program is to find the smallest number in the array, there is no compiler error but it doesn't show the correct result.
Thank you in advance!
public static void getSmall(int[] ar){
int small=ar[0];
for(int i=1; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
Setting small initially to 0 is a bad idea as you are hoping that your array contains an element less than that for the answer to be correct.
A common idiom is to initialise small to ar[0] (having, of course, first checked that ar contains at least 1 element). Then run your loop from 1.
for(int i = 1;
(I dislike initialising small to a very large number since that puts the answer to your function in an undefined state if ar does not have any elements.)
Change
int small = 0
to
int small = ar[0];
Don't init small=0,
change to int small = ar[0]
You can also use
int small=Integer.MAX_VALUE;
in place of
int small=0;
this code will print small value, give the max of integer
Change this
public static void getSmall(int[] ar) {
int small = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
to
public static void getSmall(int[] ar) {
int small = ar[0];
for (int i = 1; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
because variable small is not assigned to any value from your array