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.
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 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.
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
Why an int becomes zero when it exceed the int limit in java?
Eg:
import java.util.Scanner
public class MyClass{
public static void main(Stings [] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // enter 50 or higher.
int answer = 1;
while(x>0){
answer = answer*x;
x--;
}
System.out.println(answer);
}
}
At some point answer becomes 0 (integer overflow) and stays 0.
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 6 years ago.
Improve this question
I'm just really confused on why there's an error at the for statement line. This is literally code from a tutorial that I can't run because there's an error at line 3
class RoundOne{
public static void main(String[] args){
for(int i =0,i<10,i++){
System.out.println("The number is: "+i);
}
}
}
for(int i =0;i<10;i++){
System.out.println("The number is: "+i);
}
You wrote , instead of ;
In your for loop instead of
for(int i =0,i<10,i++)
write
for(int i =0;i<10;i++)
For loops use ; between the parts, not ,
class RoundOne {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("The number is: " + i);
}
}
}
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
What's wrong with my code?
import java.util.Scanner;
class Pali
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("\nInserisci una stringa: ");
String p = in.next();
int n = p.lenght();
}
}
I get a "cannot find symbol" error in p variable. Why? Many thanks (sorry,if I did something wrong, it's my first post).
It's a typo:
int n = p.lenght();
Should be p.length();
It is a typo.
int n = p.lenght();
Correction: int n = p.length();
It happens to the best of us.
Use: int n = p.length(); instead of p.lenght();
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.