• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is wrong with this nested loop array code?  RSS feed

 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class ChristmasProjectNestedLoops
{
String[] d = {"first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelth"};

String[] items = { "A partridge in a pear tree", "two turtle doves, and" , "three french hens," , "four calling birds," ,
"five golden rings,", "six geese a laying,", "seven swans a swimming," ,"eight maids a milking,", "nine ladies dancing,"
,"ten lords a leaping," ,"eleven pipers piping,", "twelve drummers drumming," ,};
{

for (int day = 0; day < d.length; d++)
{
System.out.println("On the %s day of Christmas, my true love gave to me, " + d[day]);
for (int item = day; item >= 0; item --)
{
System.out.print(items[item]);
}
}

}
}
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the look again. Are you incrementing/decrementing the index right?



And remember to use the code tags. *important* ;)
 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So this?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the day1 loop at all.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite.
Your first effort was closer. You only need two loops. The very first example you had a "d++" rather than a "day++" which possibly explained why it wasn't printing .

At some point you will probably want to issue a "System.out.println()"
Otherwise everything just gets written on the one line.

When a line gets really long, strange stuff can happen.
When running your above code in Eclipse, it worked, but the output was waaaaaaaay off to the right before I actually saw anything printed.
Putting in a System.out.println made it 'work'
 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So this is what I think you mean is correct, but it still won't compile? Btw thank you so much for the help
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Saying it won't compile or it doesn't work doesn't help us to help you, . Please TellTheDetails (← click)
 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I apologize, still sort of new here.

Anyways, after a bit of searching for a better compiler, I found this error:



This string: System.out.println( "On the %s day of Christmas, my true love gave to me, " + d[day] ); is a string, when it must be a localized string.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not an error I recognise.

What compiler are you using? the standard javac?
How are you compiling/running your code?
 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the compiler framework and compiling the code through eclipse. Eclipse didn't catch a single error in the code, but I knew there was one there, so I used the compiler framework. I don't recognize the error either, which is why I asked
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your 'main()' method? I had to add one to get Eclipse to compile it.
 
Arun Peter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is all this written in the class directly or in a method inside the class? I see an opening brace after the items declaration, no methods.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a big confusion going on here.

1. You do not need a main method to get this to compile, it compiles fine as is. What you do need a main method for is to get it to run.
2. The code is written as an initialiser block so it is fine (albeit it is a strange way to write the code) and will run once whenever a new object is created.

To get the code to run add a main method which creates an instance of the class and you also need to add a System.out.println(); at the end of the outer loop to prevent it all printing on one line. You also need to change the line
to a System.out.printf.

Once you've done that I suggest you move the code from an initializer block to a default constructor - it's fairly unusual to need to use initializer blocks and you certainly don't need to use them as a beginner.
 
Jeremy Schuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy crap LOL I was so focused on finding an error that I just completely forgot to add the main... biggest idiot moment in weeks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!