• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Writing main, average, max, and print methods  RSS feed

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize if this is not the right place to post these things and if this is inappropriate to ask but I'm having difficulty with an assignment in my Java Programming class. It goes like this.

I am to write four methods. The main method, an average method, a max method, and a print method. The main method is used to create an array of integers. The average and max methods are to find the average and max values of said array and the print method is to print out the methods. Here is what I have so far



Where I am getting hung up is the print method. The example in the homework shows the array separated by commas and currently, it is not doing that and it is showing only the first value of my array. Can someone please guide me as to where I am going wrong? I'm new to Java, and I'm so frustrated with myself that I can't figure this out. Thank you to anyone that can help
 
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sean,

welcome to the Ranch and enjoy the stay!

To me, it seems the commas method does all that you need. Maybe you could add a 'System.out.println();' just before exiting that method, to avoid that the next item you print is printed directly after the array.

Or did I miss something?
 
Bartender
Posts: 4525
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your main() is attempting to print the value returned from the commas() method. Your commas() method returns 'clean' which has been set to the first element of 'array'. Therefore your println() on line 8 will only print the first element. However, a side affect of your commas() method is to also print all the array elements. So you have two choices:
  • have your commas() method build a single string containing all the ints separated by commas and return that string (but not do any printing itself).
  • have your commas() method do the printing and return void. This would mean that you'd have to break the print line on line 8 into two separate calls because commas() would no longer return something to concatenate. It would also mean adding a call to System.out.println() (with no arguments) just before your commas() method returns.
  •  
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:
    Your main() is attempting to print the value returned from the commas() method. Your commas() method returns 'clean' which has been set to the first element of 'array'. Therefore your println() on line 8 will only print the first element. However, a side affect of your commas() method is to also print all the array elements. So you have two choices:

  • have your commas() method build a single string containing all the ints separated by commas and return that string (but not do any printing itself).
  • have your commas() method do the printing and return void. This would mean that you'd have to break the print line on line 8 into two separate calls because commas() would no longer return something to concatenate. It would also mean adding a call to System.out.println() (with no arguments) just before your commas() method returns.


  • I think I understand what you're saying. I need to have commas basically become my print method I need for the assignment and move all the println's to it or I need to take the output of the Commas method and turn it into a string and have it return void. I'm a little lost on how to do that though
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sean Mcintosh wrote:I need to have commas basically become my print method I need for the assignment and move all the println's to it or I need to take the output of the Commas method and turn it into a string and have it return void.

    Seeing as how your requirements are to create a print method you just need a few tweaks:
  • Rename commas() to print().
  • Have print() return void.
  • Have print() end with call to System.out.println().
  • Change how you call print() from main().

  •  
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Mcintosh wrote:I need to have commas basically become my print method I need for the assignment and move all the println's to it or I need to take the output of the Commas method and turn it into a string and have it return void.

    Seeing as how your requirements are to create a print method you just need a few tweaks:
  • Rename commas() to print().
  • Have print() return void.
  • Have print() end with call to System.out.println().
  • Change how you call print() from main().



  • I feel really dumb. I kinda understand what you're saying but I don't know how I would tweak my code to achieve this. I've changed the name of the method to print (easy enough) but when I try to change the return to void I get an error. I know I'm missing something that's probably really easy to understand
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sean, you don't always need to quote the previous post. It just makes threads longer without adding information.

    Also, when you say "you get an error" it is vitally important that your post include ALL the lines of the error message. This helps in two ways, we can get a better understanding as to where the problem is, and we can help you learn how to interpret the error messages them selves so that you might be able to figure it out yourself next time. Also, it is important to re-post your code along with the error message so that any changes that you've made to the code are in sync with the error message.

    As a blatant guess, I'd say you are probably still trying to return 'clean' from your, now 'void', print() method.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My applogies. So here is my code



    The error I'm getting is

    Error58, 16) java: illegal start of expression
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can't return 'void', just remove lines 57 and 58. You can get rid of 'clean'.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Alright. Got rid of clean but it's still asking for a return value
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have to declare that the method returns 'void' not 'int'. This is often confusing for beginners because 'void' is not a type, it is the absence of a type.

    Also, after removing 'clean' you have a straggling ';' hanging around. It won't cause compile errors or run time problems but it should be cleaned up.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok. Got that cleared out. Now I have



    What should I do now? Also thank you so much for helping walk me through this. I really appreciate the help
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sean Mcintosh wrote:

    You still need the call to System.out.println() after line 7.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok so I tried this and got an error



    It gives me an error of Operator '+' cannot be applied to 'java.lang.String', 'void'
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Two problems with what you had, you attempted to use the '+' operator with a method (print()) that returns void (ie returns nothing at all). Secondly, even if this had worked you would have ended up with an infinite recursion because you were calling print() from within the print() method.
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

  • Change how you call print() from main().

  •  
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I attempted to do that



    But I get the error it can't resolve mtIntegers. This is because it is outside of that code block yes?
     
    Sheriff
    Posts: 5029
    138
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    This is because it is outside of that code block yes?


    Yes.  This is referred to as "out of scope."  Scope is a difficult concept, but for beginners, you can usually say that if a variable is declared inside a block (between an opening and closing brace) then it is in scope for that block.
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are confusing two of my instructions, one was the change you need to make inside your main() method and the other was the change you need to make at the end of your print() method.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:You are confusing two of my instructions, one was the change you need to make inside your main() method and the other was the change you need to make at the end of your print() method.



    Yes I am very confused. I get the feeling that the print(myIntegers) doesn't go at the end of my code. That it needs to go into my main method?
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

  • Change how you call print() from main().

  •  
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok now I'm getting somewhere



    But now my output looks like this

    Enter three whole numbers.
    1
    55
    66
    My first array is:
    1, 55, 66My first array is:
    1, 55, 66My first array is:
    1, 55, 66The average of your whole numbers is 40.666666666666664
    The largest of your three numbers is 66

    What am I getting backwards?
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    You have two problems:
    You shouldn't be calling print() from inside the loop.
    You don't have a new-line after printing the last number.
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sean Mcintosh wrote:

    You still need the call to System.out.println() after line 7.

     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I'm almost there. 2 questions. How do I make a new line to separate my string "My First array is"
    and how do I call the System.out.println() after line 7?
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Calling println() without any arguments results in just a new-line being printed; no data.

    This answers both of your questions.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    YES!! It works and it looks like it should! I now have just one debugging issue



    This is my code for finding the max value. I just tested it with 1, 5000, and 44 and it says that 44 is the largest value. What didn't work there?
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are comparing 'i' to your largestValue. 'i' is the loop counter not an array value.

    P.S. When you post code be sure to include the closing braces.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Um...how do I fix that?
     
    Carey Brown
    Bartender
    Posts: 4525
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How do you get the value stored in the i'th position of an array? Hint: you're already doing that on the next line.
     
    Sean Mcintosh
    Ranch Hand
    Posts: 63
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well that was staring me right in the face lol

    I should have compared array[i] to largestvalue. now it's working correctly. Thank you so much for your help
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!