My program gives wrong output [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
When I run it, computer counts only 1 3 5 7 9 ... indexes. For example, if I enter "Hello", computer counts 1-H,1-l and 1 o, it doesn't compute e and l(4th index).
What is wrong with it?
import java.util.*;
public class LetterCount {
public static void main(String[] args){
final int Numchars=26;
Scanner scan = new Scanner(System.in);
int[] upper=new int[Numchars];
int[] lower=new int[Numchars];
char current='a';
char current0='A';
int pox=0;
System.out.println("Enter a sentence");
String line=scan.nextLine();
for(int ch=0; ch<line.length(); ch++){
for(int other=0; other<26; other++){
if(line.charAt(ch)==(char)(current+other))
lower[other]++;
if(line.charAt(ch)==(char)(current0+other))
upper[other]++;
else
pox++;
}
ch++;
}
for(int another=0; another<lower.length;another++)
System.out.println((char)(another+'a')+" " +lower[another]);
for(int b=0; b<lower.length;b++)
System.out.println((char)(b+'A')+" " +upper[b]);
System.out.println("non alphabetic characters: "+pox);
}
}

It basically boils down to:
for (int ch = 0; ch < line.length(); ch++) { // Increment per iteration
doSomething();
ch++; // Increment within body
}
in which you increment ch twice!
You need to get rid of one of them and, since the usual way to do a for loop with known-in-advance number of iterations like this is to put the control variable modification into the for statement itself, I would suggest getting rid of the one in the loop body:
for (int ch = 0; ch < line.length(); ch++) {
doSomething();
}

I think you increments ch twice, at the end of for expression and at the end of for loop.

Related

It is not going inside the first loop and only printing I am outside loop only [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 2 years ago.
Improve this question
The below code is not giving any error. After debugging i found it is not going inside the first loop.
package string_pattern;
public class Naive {
public static void main(String[] args)
{
String pattern="This is a text book";
String text="text";
child obj=new child();
obj.search(pattern, text);
}
}
class child{
public void search(String pat,String text)
{
int m,n;
m=pat.length();
n=text.length();
System.out.println("i am outside loop");
for(int i=0; i<= (n-m); i++)
{
int j;
System.out.println("i am inside first loop");
for(j=0;j<m;j++)
{
if(text.charAt(i+j) != pat.charAt(j))
{
System.out.println("OVER HERE");
break;
}
if(j==m)
System.out.println("pattern found at pos" + i);
}
}
}
}
You do the following:
m=pat.length();
n=text.length();
The problem is that text has a length of 4, while pat is longer. So when you do the loop like this -> for(int i=0; i<= (n-m); i++), you end up with a negative number. Since 0 is greater than any negative number you never enter the loop.

Wierd String error, please be gracious, new to 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 my problem is that it keeps saying I already defined String spaces up top but if I take the definition that's outside of the loop away and try to define spaces only in the loop, it tells me it hasn't been instantiated...
public class NestedLoop {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
int j = 1;
String spaces = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Enter userNum:");
userNum = scnr.nextInt();
for (i = 0; i <= userNum; i++) {
while (j == i){
spaces = spaces.concat(" ");
System.out.print(spaces);
j++;}
System.out.println(i);}
Error: main.java:244: spaces is already defined in main(java.lang.String[])
I just ran this code in my IDE and got no errors, compile time or run time. Not sure I can replicate the errors you're reporting. Only possibility I could think of is an error with your JDK, but at the same time I dunno how or why that would occur.

Java When I run this method in my code, sometimes it shows full result, other times it doesn't [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 7 years ago.
Improve this question
If this function is running:
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
int random = r.nextInt(A.length);
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
if(A[random]==0)
A[random] += 1;
}
}
}
The output some times breaks a bit or freezes eclipse, totally at random, and sometimes it is able to give the whole result, other times it doesn't show the last part of the desired result.
If your A[random] != 0 and still your totalweight() returns < 630, the while loop would be infinite. One possible fix (and the one I think you need) is to move your int random = r.nextInt(A.length); inside your while loop.
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
int random = r.nextInt(A.length);
if(A[random]==0)
A[random] += 1;
}
}
}
Note: This would still loop infinitely if the sum of weight array is still < 630. So you will need additional checks.

Problems with displaying multi array [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
I want to display multi arrays.
public class apples {
public static void main(String[] args){
int Tabela_Nr1[][] = {{8,9,10,11}, {12,13,14,15}};
int Tabela_Nr2[][] = {{30,31,32,33}, {43},{4,5,6,}};
System.out.println("To jest pierwsza tabela: ");
wyswietl(Tabela_Nr1);
System.out.println("To jest druga tabela: ");
wyswietl(Tabela_Nr2);
}
public static void wyswietl(int x [][]){
for (int row = 0; row < x.length; row++){
for(int counter = 0; counter < x[row].length; counter++);
System.out.print(x[row][counter] + "\t");
}
System.out.println();
}
}
Debugger tells me that variable counter is not declare, but it is in "for" loop...
What is the cuz of the problem ?
Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
counter cannot be resolved to a variable
at apples.wyswietl(apples.java:15)
at apples.main(apples.java:8)
The problem is the extra semi colon :
for (int row=0;row<x.length;row++){
for(int counter=0;counter<x[row].length;counter++); //<-- here
System.out.print(x[row][counter]+"\t");
This semi colon end the inner loop, which means the print statement is outside the loop, where counter is unknown.
I think that your System.out.println(); is also wrongly placed. You probably want to place it after each row :
public static void wyswietl(int x[][]){
for (int row=0;row<x.length;row++) {
for(int counter=0;counter<x[row].length;counter++) {
System.out.print(x[row][counter]+"\t");
}
System.out.println();
}
}
for(int counter=0;counter<x[row].length;counter++); --> remove the ; and it will work just fine :)
BTW you might also want to look into --> Arrays.deepToString() which displays multi-dimensional arrays.

Variable not found 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 9 years ago.
Improve this question
I've just started working with some programming in java. I'm experimenting with loops and I have a problem with a for-loop where I'm having a hard time finding the mistake. Its saying that "i" is not a variable, even though i made it one in just above. Hope you guys can help!
import java.util.Scanner;
public class Loops {
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Skriv et tal");
int a = sc.nextInt();
for(int i = 0; i <= 9; i++);
{
System.out.println(a + i);
}
}
}
for(int i=0; i<=9;i++);
// ^ get rid of this
should be
for(int i=0; i<=9;i++)
Because of that the for statement ends there and the new block had been started there.
Beware of the ; behind your loop definition. It is an empty statement.

Categories

Resources