• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Arrays in range

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is my new program. I am suppose to find the ranges of a list of numbers the user inputs. My program is a mess but i know im on the right track. Can someone please help me make it compile. I have so many errors I dont know how to go about fixing it.




 
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clean up your indentation and you will see that you have code that is not contained inside a method.
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Clean up your indentation and you will see that you have code that is not contained inside a method.




I cleaned it up and added some stuff. When i run it, it seems to be printing out multiple times. I also want the asterisk to be printed out not actual numbers





 
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is what properly indented code should look like. There are two competing brace placement styles: K&R, and Allman. For beginners I find that Allman leads to fewer errors.

On line 28 you have a loop for incrementing the counts. Before you add the logic for printing you should complete all the counting first.

Note that on lines 70 and 71 you have blocks (braces) not associated with any conditional logic. These are unnecessary and make debugging your code more difficult. You'll have to delete the matching closing braces as well.
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

This is what properly indented code should look like. There are two competing brace placement styles: K&R, and Allman. For beginners I find that Allman leads to fewer errors.

On line 28 you have a loop for incrementing the counts. Before you add the logic for printing you should complete all the counting first.

Note that on lines 70 and 71 you have blocks (braces) not associated with any conditional logic. These are unnecessary and make debugging your code more difficult. You'll have to delete the matching closing braces as well.




Thank you so much! I do have one last question though.

I am trying to Nest my for loops

First for loop goes through array

Second for loop inside of the first loop adds a * as long as the int is less than the number at the index defined in your first for loop. I thought I did it right but for some reason now it doesn't print back the numbers the user typed in. by nesting my loops it was suppose to have the *** print out rather than numbers.

 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to point out that you can't use nested loops. You have to do all the counts first, and only then, have nested loops to do the printing.

Note, if you copied the indented code I posted, I had used tabs to indent the code. I then updated the post using only spaces. So if you need/want to, then grab it again.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I was trying to point out that you can't use nested loops. You have to do all the counts first, and only then, have nested loops to do the printing.

Note, if you copied the indented code I posted, I had used tabs to indent the code. I then updated the post using only spaces. So if you need/want to, then grab it again.



Okay but when i move the first for loop after the user input i get 40 errors. How do i do the counts first?
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:




this is not helping me understand.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, the first error listed is the root of the problem. Fix that and probably the other errors will go away.

I can't be more specific without you reposting the current state of your code and the complete error message.
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:In general, the first error listed is the root of the problem. Fix that and probably the other errors will go away.

I can't be more specific without you reposting the current state of your code and the complete error message.







no more errors and now for loops are at the bottom. only issue now is the *** is not printing out
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually the other problem is that now it doesnt collect the # and put them in its ranges
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dominique Cadenas wrote:

Carey Brown wrote:




this is not helping me understand.


Regarding this: The compiler could care less, this is strictly for your benefit. And it seems that your inconsistent indentation is one of the causes of your confusion. I'm only pointing this out because I think it will help you see some of your own mistakes, and that others expect to see Allman or K&R. In forums like this, proper indentation shows you care enough about your own code to put this effort in, so others are more likely to volunteer their efforts to help you.
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Dominique Cadenas wrote:

Carey Brown wrote:




this is not helping me understand.


Regarding this: The compiler could care less, this is strictly for your benefit. And it seems that your inconsistent indentation is one of the causes of your confusion. I'm only pointing this out because I think it will help you see some of your own mistakes, and that others expect to see Allman or K&R. In forums like this, proper indentation shows you care enough about your own code to put this effort in, so others are more likely to volunteer their efforts to help you.




Oh I see. Thank you so much! I see the difference and I will use the Allman structure for if statements
 
Dominique Cadenas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Dominique Cadenas wrote:

Carey Brown wrote:




this is not helping me understand.


Regarding this: The compiler could care less, this is strictly for your benefit. And it seems that your inconsistent indentation is one of the causes of your confusion. I'm only pointing this out because I think it will help you see some of your own mistakes, and that others expect to see Allman or K&R. In forums like this, proper indentation shows you care enough about your own code to put this effort in, so others are more likely to volunteer their efforts to help you.




can you please help me so that it just prints out tt=he chart once not as many times as the list the user puts in
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dominique Cadenas wrote:can you please help me so that it just prints out tt=he chart once not as many times as the list the user puts in



You want to make sure that the "Print the chart" code is not inside a loop which runs once for each list entry.

I can't help you with that because so far you have only posted code which is unreadable because of the indentation. My suggestion to you would be to indent your code (as per Carey's suggestions) and then see what loops your "Print the chart" code is inside.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Going back to my indented sample, you'll need a closing brace after line 67. This will enclose the counting loop prior to the printing logic, which still needs some work. For printing you'll need nested loops, one to go through the array, and another to print the asterisks.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dominique Cadenas wrote:Oh I see. Thank you so much! I see the difference and I will use the Allman structure for if statements


Although I showed examples using "if()" this applies to ANY code block enclosed in braces.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, I don't know what editor you are using to write your Java code, but there are editors that make indenting easier. Notepad++ has been mentioned by some as a good choice. Of course IDE's like Eclipse can handle all sorts of formatting options but beginners are discouraged from using those in the beginning because they have a somewhat steep learning curve and its enough just to learn Java without also trying to learn an IDE. Notepad++ may be a reasonable compromise for you.

(But not tonight.)
 
Marshal
Posts: 76866
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . editors that make indenting easier. Notepad++ has been mentioned . . .

I have used Notepad++ and it is very good.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic