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.
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 3 years ago.
Improve this question
I just started learning java, and I created a support code (called HELP) to help me track some variables in another code im writing. But when I try to run HELP I get this exception in return, can someone help me?
Im using INTELIJ
public static void main(String [] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int T = Integer.parseInt(args[2]);
for (int i = 0; i < T; i++) {
//bob vĂȘ a carta
int see;
int unseen;
if (Math.random() > .5) {
see = a;
} else see = b;
System.out.println(see);
}
}
the output is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at HELP.main(HELP.java:4)
You aren't specifying any arguments when you run the program so args[0], args[1], args[2] isn't a valid index.
In one old post founded this
// to use 10 when there aren't args...
int trials = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
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 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.
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.