-1

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[])

David
  • 89
  • 9
  • Strings are immutable in Java. Check: http://stackoverflow.com/questions/1552301/immutability-of-strings-in-java – Baderous Feb 21 '16 at 20:16
  • @Baderous Has nothing to do with immutability here. However, what is the error with the current code? – Tunaki Feb 21 '16 at 20:17
  • main.java:244: spaces is already defined in main(java.lang.String[]) – David Feb 21 '16 at 20:17
  • also, main.java:259: spaces is already defined in main(java.lang.String[]) – David Feb 21 '16 at 20:18
  • Then you've already defined a variable. You can't re-define a variable. In the posted code, `spaces` is only defined once so this can't throw the error... – Tunaki Feb 21 '16 at 20:18
  • what I'm trying to do is add `i` spaces before I print `int i` and then add a new line. How would I go about that? – David Feb 21 '16 at 20:21
  • This is only part of the code. You are not showing the part of the code after the loops. Please [edit] your question and mark with a comment which line is 244 and which line is 259. Also, add the errors to the question itself, this is important information and shouldn't be lost in the comments. – RealSkeptic Feb 21 '16 at 20:22
  • Your code runs fine, though indentation sucks, and it won't do much with `userNum = 0`. Changing `userNum` to a higher value will print more, but probably not what you expected. See [IDEONE](https://ideone.com/hesnap) for result. – Andreas Feb 21 '16 at 20:37
  • The site itself defines values for `userNum` so I didn't need to worry about that. – David Feb 21 '16 at 20:39
  • and yeah, I fixed the loops, thanks – David Feb 21 '16 at 20:45

1 Answers1

0

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.

RJM
  • 73
  • 4
  • I'm using a site called Zybooks (virtual textbook site). So it's probably just a stupid error with the site. Thanks – David Feb 21 '16 at 20:29
  • @David See [IDEONE](https://ideone.com/hesnap) for running version of your code, and results when setting `userNum = 3`. – Andreas Feb 21 '16 at 20:39
  • Yup, just edited and fixed it, thanks – David Feb 21 '16 at 20:45
  • If you're new to Java programming and are still looking for a good IDE to work with, I recommend JetBrains IntelliJ. Community edition is free and has everything you'll need to write and debug code. Link: https://www.jetbrains.com/idea/ – RJM Feb 23 '16 at 05:22