Forums Register Login

Tables in Java

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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...

+Pie Number of slices to send: Send
Welcome to the Ranch.

What have you done so far? People tend to answer more when you ShowSomeEffort (← click). And when you do post some code, please UseCodeTags.

Edit: I put the code tags in this time, and some formatting.
+Pie Number of slices to send: Send
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.....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
This program is a program of an array of 20 integers that shows random numbers when the program is running.
+Pie Number of slices to send: Send

You declared the "samples" local variables three times in the same method -- which is not allowed.

Henry
+Pie Number of slices to send: Send
Penny Paga, hi.

Would you like to explain line by line, what do you think you're doing, starting from line 5 up to line 9?
+Pie Number of slices to send: Send
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....
1
+Pie Number of slices to send: Send
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?


1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Please post the current code you have, as we can interpret wrongly.
+Pie Number of slices to send: Send
 

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.





+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I meant I deleted the pairs of the curly braces, forgot to write the word pairs. Yes I know they always go in pairs
+Pie Number of slices to send: Send
We have some suggestions about formatting here on CodeRanch.
+Pie Number of slices to send: Send
Thank you so much Campbell!
+Pie Number of slices to send: Send
You're welcome
+Pie Number of slices to send: Send
 

Carey Brown wrote:Java supports multidimensional arrays, a 2 dimensional array could be considered a table.


Java does not support multidimensional arrays. Java only supports arrays of arrays.

Two-dimensional array: int[,] foo = new int[4,6];
Array of arrays: int[][] bar = new int[4][6];
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:Remember:
1. Class names all the time suppose to start with the first Capital letter.
...



Also, line 8 doesn't make sense in your program at the moment.
+ Still bad whole code formatting.
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
Any suggestion how to make it right?..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
The loop we can use here is the loop for right?
+Pie Number of slices to send: Send
Yes, you can achieve that by using for loop.
Small tutorial how it works > here.
3
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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.



Thanks a lot! So wonderful!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Jesper de Jong wrote:

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...
+Pie Number of slices to send: Send
Penny, look at the example in the tutorial here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

It should give you an idea of how you can display each element in the array.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
I think you will find a better discussion of random ints here, even though I am blowing my own trumpet.
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1908 times.
Similar Threads
Input/Output Question...
Java applets
Help! How does 'high school' teaching work?
Graduate Program
Cleared 1Z0-803 with 90%
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:22:57.