Hello people!
I'm a beginner in Java, started a new semester at school and we started java, untill now I went well, but we started learning about tables and have a little program.
The teacher gave us an excercise and doesn't works (not running), I receive too many errors.
So can anyone just post a simple java program with tables with 20 numbers, that is giving random numbers?
I feel so silly it looks easy but can't make it successfully.....
Thank you
Penny Papa wrote:Hello people!
I'm a beginner in Java, started a new semester at school and we started java, untill now I went well, but we started learning about tables and have a little program.
The teacher gave us an excercise and doesn't works (not running), I receive too many errors.
So can anyone just post a simple java program with tables with 20 numbers, that is giving random numbers?
I feel so silly it looks easy but can't make it successfully.....
Thank you
Penny
When you say "tables" it is not clear what you mean. Java supports multidimensional arrays, a 2 dimensional array could be considered a table. Java Swing does have a JTable class but now you're into the world of Swing, is this what you mean?
Oh yes I'm sorry I meant arrays. For now I'm interested in simple arrays...
I have a program but is running an error... I'm new in this just can't understand what's wrong...
Thank you Knute..
I'm new here and new in Java so I'm trying to be in the "mood"
I totally understand. I've done so far the classic Hello program, and factorial, and a program that shows something name, color etc...
In this code I posted I'm deleting some things to see if it works and nothing works, so I left it as I wrote it from the class... It's probably wrong, but have no idea what is wrong.....
Well, the example doesn't even compile. What is it you're trying to do? Either write down in English (or your native language) what it is you're trying to do, or remove everything from the code and just do one thing correctly. Then we'll go from there.
Hello Liutauras...
I don't know exactly to explain the whole code is how I saw it in the class and wrote it down in my notepad..
But I can explain some that I understand
the line 5 is a new array we want to create by the name samples and has 20 integers
I think line 6 is wrong but not sure..
line 7 have no idea
and also 8 and 9.... obviously the code is wrong so I have to delete some rows again to see how it works....
I think what you have here is several independent examples of how to declare and use arrays.
They don't work all in the same program
Which is why you get complaints about duplicate named variables.
Here is a version of it split into examples. I added code blocks so it doesn't complain about 'redeclaration' of variables.
The last bit - the loop belongs to one of these examples... which one?
Penny Papa wrote:the whole code is how I saw it in the class and wrote it down in my notepad..
Good, I see a positive, when you come out of the class you try to do practise. You're on the right path.
Penny Papa wrote:I feel so silly it looks easy but can't make it successfully...
Everything is easy when you master it. Don't force yourself, it takes time.
Stefan Evans gave you a wonderful picture.
You mentioned, that some of the parts of the code you do understand. So keep what you understand and remove what you don't.
Then try to take Stefan's given hint, research and see, does it makes sense or not, if not - move towards another "example" mentioned in Stefan's post until you find right one.
Thank you Stefan and Liutauras!
Stefan I did these examples but now there's only one error and that one is about the line 30 samples[i] = i;
the error says cannot find symbol samples[i] = i;
What does it means?
Penny Papa wrote:Thank you Stefan and Liutauras!
Stefan I did these examples but now there's only one error and that one is about the line 30 samples[i] = i;
the error says cannot find symbol samples[i] = i;
What does it means?
I left a comment above that bit of code. Did you understand the comment?
"Cannot find symbol" means that the code uses a "name" that the compiler can't find.
At this point in the code, there is no variable called "samples" in scope for it to use.
In java, a variable is only available in the block in which it is declared (i.e. between curly brackets), and because there are blocks around all of the above examples declaring "samples" then there is no declaration at this point where it is being used in the loop.
This code NEEDS a declaration of the samples array to run. There are several examples existing in the code. Which one of those that you wrote down would match with this for loop?
You either need to declare "samples" in the same block as the for loop, or move the loop code up into one of the previous examples.
Ok. Now you reached the point, where correct naming and indentation takes very important steps.
Remember:
1. Class names all the time suppose to start with the first Capital letter. [Make correct]
2. Curly braces indicates where block's of statements starts and where finishes, in fact, the scopes of variables are defined. [Indent your code properly]
3. Post corrected code again. [Below you'll find hint]
Nicely formatted code usually gives you an answer where code indentation goes wrongly.
Thank you Liutauras!
But I still got error, seems is difficult for me to know where to put the curly braces.
I deleted the curly braces one to one but still I got errors...
Don't add or delete a brace. Always use them in pairs, which entails what I call writing backwards. There is an example here and I have some suggestions about text editors here. Use a decent text editor, as I suggested. Programming is difficult enough as it is, without spending hours and hours hunting little formatting errors which stop your code from working.
You may find it is quicker to get one of the good text editors I mentioned or look in our FAQ, and write the code again from scratch, than to try correcting it by hand on a poor editor.
Finally it worked!
I found out which curly braces pair to delete! This is the correct program!
Thanks to anyone that helped me!
But I'm sure I will have new questions again!
True about line 8 it runs 20 times showing this only without numbers. But at least I had no errors and the program runs...
Ok yes I remembered after I posted I had to have the first letter of the class capital....
As you probably know, array elements can be accessed by their indexes.
Element 1 at index 0 - Array[0]
Element 2 at index 1 - Array[1]
Element 3 at index 2 - Array[2]
In order to print them, of course you won't write each time separate statement to print element.
You need to find the way, how to print them using a loop, so you could iterate through the index.
Hint: your current code is your hint.
Try to find the way and post it here.
Penny, this is how you should format and indent this code so it's easy for people, including yourself, to read:
Indentation is by "levels" and in Java, each level is usually indented by four spaces. Indentation helps show how different parts of the program are organized and executed.
Line 3: The class Pinakes (note the capitalization) is not indented because it is the topmost structure; It is at Level 0.
Line 5: The main method is indented four spaces to show that it is a part of the Pinakes class. We say that "The main method is indented by one level in class Pinakes" or that it is at the first level of indentation.
Lines 6 & 8: These statements are executed as part of the main method and are thus indented by another four spaces. These are at the 2nd level of indentation.
Lines 9 & 10: These statements are executed as part of the main method as well but they are specifically executed as part of the for-loop on line 8. These are again indented another four spaces to show another level of organization, at the 3rd level.
Lines 11, 12, 13: The closing braces for the different levels. Note how they are aligned to the level of the statement block that they close. That is, the brace on line 11 closes out the for-loop block of statements, so we indent it to the same level as the for statement on line 8. The brace on line 12 closes out the main method so again we indent it to the same level as main method on line 5. Finally, the brace on line 13 closes out the class Pinakes, so we give it the same level of indentation as line 3, at level 0.
If you follow this style consistently, your programs will be easier to read and you will be less likely to get confused as to which statements will be executed together as a unit of work.
Junilu Lacar wrote:Penny, this is how you should format and indent this code so it's easy for people, including yourself, to read:
Indentation is by "levels" and in Java, each level is usually indented by four spaces. Indentation helps show how different parts of the program are organized and executed.
Line 3: The class Pinakes (note the capitalization) is not indented because it is the topmost structure; It is at Level 0.
Line 5: The main method is indented four spaces to show that it is a part of the Pinakes class. We say that "The main method is indented by one level in class Pinakes" or that it is at the first level of indentation.
Lines 6 & 8: These statements are executed as part of the main method and are thus indented by another four spaces. These are at the 2nd level of indentation.
Lines 9 & 10: These statements are executed as part of the main method as well but they are specifically executed as part of the for-loop on line 8. These are again indented another four spaces to show another level of organization, at the 3rd level.
Lines 11, 12, 13: The closing braces for the different levels. Note how they are aligned to the level of the statement block that they close. That is, the brace on line 11 closes out the for-loop block of statements, so we indent it to the same level as the for statement on line 8. The brace on line 12 closes out the main method so again we indent it to the same level as main method on line 5. Finally, the brace on line 13 closes out the class Pinakes, so we give it the same level of indentation as line 3, at level 0.
If you follow this style consistently, your programs will be easier to read and you will be less likely to get confused as to which statements will be executed together as a unit of work.
Penny Papa wrote:Finally it worked!
I found out which curly braces pair to delete! This is the correct program!
Thanks to anyone that helped me!
But I'm sure I will have new questions again!
Is this really what you wanted the program to be?
When you run this program, the only thing you will see it is that prints "it's: " 20 times. You are putting values in the samples array, but you are not doing anything with the array after that.
Penny Papa wrote:Finally it worked!
I found out which curly braces pair to delete! This is the correct program!
Thanks to anyone that helped me!
But I'm sure I will have new questions again!
Is this really what you wanted the program to be?
When you run this program, the only thing you will see it is that prints "it's: " 20 times. You are putting values in the samples array, but you are not doing anything with the array after that.
True. No is not what I actually wanted. It runs 20 times the phrase "it's" without any values. But I was happy it run without any error, after too many errors I was receiving...
I'm a beginner and didn't know what to do to make it run without seeing errors, so I was happy I finally made it without receiving any error. But not satisfied by the result I saw...
Junilu Lacar wrote:You are almost there. Just add a couple of lines of code and change a couple of the lines that you already have and you should be done.
When you have a feeling that you won't reach the end of the tunnel, here he is, JUNILU steps in, then instantly you start seeing light at the end of the tunnel