• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Begginer to Java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just started learning Java and I have very little coding experience prior to now. I'm 17 and a junior in high school, but I'm doing this on the side without any teaching or guidance. I was hoping I could get some sort of help here as it is challenging to learn on my own.

As of right now I have only been making small math programs such as a Dice roller, Times Table Maker, a program to find the area of a rectangle with the dimensions given by the user, and I'm working on one right now that is giving me some issues. It is a program that creates a Times table with the user choosing a number, and the multiple they wish for the table to go to.

My current issue is finding a way to display the multiples from an array.

The "for" loop is what puts the multiples into the array, but I need to add something to the end to actually display them to the user.

Would appreciate any help that I can get.

Thanks in advance.

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please be specific about what you want; "Begginer to Java" doesn’t help us to find what the question is about. Avoid long lines; I had to change your post, and you can see there a better way to write long Strings in code.
I hope you are only using that long main method to try out the multiplying method, because main methods are meant only to start off your application.
I suggest you create a displayArray method, which might well go into a utility class. Search this forum and “Java in General” for “Utility Class” for user number = 109202, date = any, and you will find incomplete examples of utility classes.At least two ways to do it:
  • 1: Use a loop
  • 2: Find methods of this class which can turn an int[] into a String.
  • Because arrays don’t have overridden toString() methods, you can’t simply write system.out.println(myArray);
    There is something strange about your loop with 2 and index - 2 in. Did you have difficulty writing that loop?
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Test your method like this
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charlie Hauk wrote:Would appreciate any help that I can get.


    Well, just off the top of my head, I'm wondering what:
    int end[] = new int[intfinish - 1];
    is for.

    Simply put: it's wrong (well OK, not necessarily wrong; but certainly overcomplicated) - but I was wondering why you thought you needed to do it.

    Winston
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Charlie Hauk wrote:Would appreciate any help that I can get.


    Well, just off the top of my head, I'm wondering what:
    int end[] = new int[intfinish - 1];
    is for.

    Simply put: it's wrong (well OK, not necessarily wrong; but certainly overcomplicated) - but I was wondering why you thought you needed to do it.

    Winston



    That is how I set the amount of cells in the array. It took the finish value that the user inputs (2-50) and stubtracts one since the arrays start at 0.
    So for example, if they entered a finish value of 12, we need 11 cells in the array. That's the only way I figured out how to do it.

    I have to leave so I don't have any time, but I'll be back later on today.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charlie Hauk wrote:It took the finish value that the user inputs (2-50) and stubtracts one since the arrays start at 0.


    The starting at 0 bit is irrelevant because you're specifying a length.

    So for example, if they entered a finish value of 12, we need 11 cells in the array.


    Now that I can understand, but it's really not necessary. So what if you never use the first element of the array? It's only 4 bytes.
    And doing it your way, you'll be forced to adjust all the index values you use.

    Tip: The simplest way (providing it works) is almost always the best.

    Winston
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Did you have difficulty writing that loop?



    No, I didn't. The way I was understanding it as I wrote it was that I needed the first multiple to be stored in the first cell (0), which would be the start value - 2. From there the start value would add 1 upon repeat of the loop, and choose the next cell in the array. I'll check out the things that you mentioned.

    Winston Gutkowski wrote:And doing it your way, you'll be forced to adjust all the index values you use.

    Tip: The simplest way (providing it works) is almost always the best.



    I'm able to be on here from school, at the moment. But as far as the simplest way, this is the first thing that came to my head.
    But at this time I have very minimal knowledge of Java, or even programming in general so I hope I can learn a lot here.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charlie Hauk wrote:But as far as the simplest way, this is the first thing that came to my head.


    No, the length came into your head. But what about when you come to access those values? (which you're likely to do a lot more than setting up the array)

    For a times table for x, wouldn't it be a lot easier to simply have the value for x*n in table[n]?
    OK, you probably won't use table[0] or table[1] very often, but does that matter? Memory is cheap, and you can certainly plug in values that make sense, even for those entries.

    It does mean that you have to consider that index 0 when calculating the length, however; but I'll leave that up to you.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So that is why you have the - 2. That loop looked like a loop which had been repeatedly tweaked because it kept running out of indices. Agree with Winston: include 1 × and 0 × in your times table.
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:No, the length came into your head. But what about when you come to access those values? (which you're likely to do a lot more than setting up the array)

    For a times table for x, wouldn't it be a lot easier to simply have the value for x*n in table[n]?
    OK, you probably won't use table[0] or table[1] very often, but does that matter? Memory is cheap, and you can certainly plug in values that make sense, even for those entries.

    It does mean that you have to consider that index 0 when calculating the length, however; but I'll leave that up to you.

    Winston



    Oh... I see now. See, I misunderstood the arrays I guess. I was thinking that they HAD to start on the 0 cell. DERP!
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charlie Hauk wrote:Oh... I see now. See, I misunderstood the arrays I guess. I was thinking that they HAD to start on the 0 cell. DERP!


    They do. But there's no God-written rule that says that you have to USE all those cells.

    Winston
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Charlie Hauk wrote:Oh... I see now. See, I misunderstood the arrays I guess. I was thinking that they HAD to start on the 0 cell. DERP!


    They do. But there's no God-written rule that says that you have to USE all those cells.

    Winston



    Ah! I think I understand what you mean. You can never have too much space!
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:At least two ways to do it:

  • 1: Use a loop
  • 2: Find methods of this class which can turn an int[] into a String.
  • Because arrays don’t have overridden toString() methods, you can’t simply write system.out.println(myArray);
    There is something strange about your loop with 2 and index - 2 in. Did you have difficulty writing that loop?


    I made a method to display the array, but how do I use variables from my main method in this Utility class? When I try to use them, it isn't recognizing them as variables. The start and infinish variables cannot be found.

     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, nevermind, I figured it out. This is what I did:

    The error is now gone and it prints the values just as I wanted them when I run the program.
    Though, I would like to have the values display on a popup window.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have got some poorly‑named identifiers there, including the name of the method. That does not print an array, but only part of it. And what is going to happen if I do this:-What is more, you cannot put that on a component of any kind. That method does not do what you want at all.
    You were right to look for a toString method, so you need to use some sort of toString method. Do you have to write your own, or can you look for a utility class which might have such a method ready‑made?
     
    Charlie Hauk
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:You have got some poorly‑named identifiers there, including the name of the method. That does not print an array, but only part of it. And what is going to happen if I do this:-What is more, you cannot put that on a component of any kind. That method does not do what you want at all.
    You were right to look for a toString method, so you need to use some sort of toString method. Do you have to write your own, or can you look for a utility class which might have such a method ready‑made?


    I made it to display the numbers based on this main class:

    It's working just fine, though. I did see a sort method, but I wasn't sure how it worked, so I just made that class to display the array values... This is a lot to take in.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic