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 5 years ago.
Improve this question
public class Sum_of_Numbers {
public static void main( String [] args) {
int sumOfEven = 0;
int sumOfOdd = 1;
int even_Times = 0;
int odd_Times = 0;
while ((even_Times < 12) || (odd_Times < 13)); {
sumOfEven = sumOfEven+2;
even_Times = even_Times+1;
sumOfOdd = sumOfOdd + 2;
odd_Times = odd_Times + 1;
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
}
When I run this code, the loop fails to start and I don't know why.
You've used the wrong syntax with the while statement and it's in an infinite loop
while ((even_Times < 12) || (odd_Times < 13)); {
The semi-colon is closing the statement, so only the conditions within the while loop are executed. even_Times and odd_Times don't increment, so it loops forever.
When the semi-colon is removed, the following { } block will execute within the while loop.
while ((even_Times < 12) || (odd_Times < 13)) {
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 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 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 8 years ago.
Improve this question
I am practicing and i need help with this code . I need to read integers from the keyboard and print how many are positive Any help in what im doing wrong in my code below?
int size = 10;
int count = 0;
int cuenta = 0;
int[] numbers = new int[size];
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter 10 digits: ");
while (count < size) {
numbers[count] = keyboard.nextInt();
count++;
}
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] >= 0) {
cuenta++;
System.out.println("There are " + cuenta);
}
}
}
}
You have your logic to check for positive integers right. To point you in the right direction think about your print statement and whether it need to be within the for loop.
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] >= 0) {
cuenta++;
System.out.println("There are " + cuenta);
}
}
you need to print out the count after for loop so that it has right answer
System.out.println("There are " + cuenta);
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
my code is trying to find same 3 letter on gene[] and print corresponding array on trait[]. but the "if functions tries to check if the whole array input is divisible by 3".. need help why my "if" function is not printing!!!
public class HelloWorld {
public static void main(String[] args) {
//args example: TTTGGTGGGTTC
String[] gene = {"TTT","TTC"........GGC","GGA","GGG"};
char[] trait = {'F','F','V','V'......,'E','G','G','G','G'};
String input = args[0];
int dome = input.length();
int x = 0;
int z = 2;
int b = 0;
for (int c = 0 ; c <= dome/3 ; c++){
String top = input.substring(x, z+1);
while (!top.equals(gene[b]) ){
b = b + 1;
}
System.out.print(trait[b] + " ");
x = x + 3;
z = z + 3;
b = 0;
}
if ( dome%3 == 0){
System.out.print("no excess ");
}else{
System.out.print("*");
}
}
}
I think your for loop runs one index too many. Instead of
for (int c = 0 ; c <= dome/3 ; c++)
try
for (int c = 0 ; c < dome/3 ; c++)
If your if/else part is not working then,there might be an exception is thrown from inside For loop.
Because the for loop updates nothing,by which the if/else part are affected.
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
Here is some basic code:
public static void main(String[] args) {
String string = "Hello!";
System.out.println("First loop.");
for (int i = 0; i < string.length(); i++) {
System.out.println("g");
}
System.out.println("Second loop.");
for (int i = (string.length() - 1); i <= 0; i--) {
System.out.println("g");
}
}
For some reason, the program won't go through the second loop at all. This is somewhat strange. Can you explain this, and how to fix it?
Your second loop should be looping backwards, while the index is still greater than or equal to zero, not less than or equal to zero. With <= 0, i is greater than zero on the first evaluation and the loop never runs.
Try:
for (int i = (string.length() - 1); i >= 0; i--) {
Change the for condition,the i is initial with value greater than 0 (length-1) and there is condition i <= 0 which is true in case length is equal to 1.But the length of string is 6 so change the condition as below :
for (int i = (string.length() - 1); i >= 0; i--) {
System.out.println("g");
}
Your problem is the condition in the second for loop
i <= 0
never happens. I don't understand why you would want to check that.
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
Below is a simple program which searches for sequential characters. However I get an exception because I'm searching outside of the array index. I understand why it occurs but unsure how do I manage this?
public static void main(String [] args)
{
String term = "Popeye's fishCat";
String query = "P's SalmonCat";
int score = 0;
char [] termChar = term.toCharArray();
char [] queryChar = query.toCharArray();
if((queryChar[0]) == (termChar[0]))
{
score++;
}
for(int i = 0; i < queryChar.length; i++)
{
for(int j = 0; j < termChar.length; j++)
{
if(queryChar[i] == termChar[j] )
{
if((queryChar[i + 1]) == (termChar[j + 1])) //Causes an exception
{
System.out.println((queryChar[i + 1]) + " " + (termChar[j + 1]));
score++;
break;
}
}
}
}
System.out.println(score);
}
Adjust your loop conditions:
for(int i = 0; i < queryChar.length - 1; i++) {
for(int j = 0; j < termChar.length - 1; j++)
Note the added -1
If you always look at the next item in the loop, you can't loop to the last item, because it doesn't have a next item.
Just remove i +1 or j+1 in this bit
if((queryChar[i + 1]) == (termChar[j + 1])){
System.out.println((queryChar[i + 1]) + " " + (termChar[j + 1]));
score++;
break;
}
As legnth returns the size of the array, counting the first element as 1, but when getting elements from an array, the first is labeled zero. Using length - 1 ( the highest possible value in that loop, as when i = length the loop stops), and then adding 1, tries to refer to array[length], which doesn't exist. As it starts at zero, so the last element is array[length-1]