• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tables in Java  RSS feed

 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program is a program of an array of 20 integers that shows random numbers when the program is running.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the current code you have, as we can interpret wrongly.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant I deleted the pairs of the curly braces, forgot to write the word pairs. Yes I know they always go in pairs
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have some suggestions about formatting here on CodeRanch.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Campbell!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Penny Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion how to make it right?..
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Papa
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loop we can use here is the loop for right?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can achieve that by using for loop.
Small tutorial how it works > here.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
Android Java Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And then look at examples for using the Java Random class
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will find a better discussion of random ints here, even though I am blowing my own trumpet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!