• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Proper use of variables in methods being passed to main?

 
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I return the value of only one behavior if that particulare behavior is true. My code reads:

The group Name is: The Killers
The Killers are Zack Chris Frank Nick
Kissing his girlfriend.
What is Frank? Frank is nullKissing his girlfriend.nullnull

I need to only print one of the behaviors that are randomly generated not the ones that contain null.




 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get the output you did with the code you posted. However, here are a few suggestions:

* In Behavior, you are setting variables inside your System.out.println(). This is legal, but not advisable (see my signature). The variables aren't being used anywhere else.

* You still need to indent your code properly and remove extra blank lines. One blank line is enough.

* Student has a field that's never used: behavior.

* In Behavior, your for loop only executes once. Is this what you wanted?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ross Gerard wrote:My code reads:


Yes, but it doesn't read very well. I've removed the tons of blank lines you had, and put your classes in separate blocks, but I can't do much about your indenting, which is terrible.

I'm sure some of it is lack of familiarity with our forums, but you really should try to write code as neatly as you can, because it will save you a lot of problems later on (or maybe even now).

You can use the 'Edit' key to see what I've done; and please read the UseCodeTags (←click) page for information on how to use code tags properly.

Thanks, and welcome to JavaRanch.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have some suggestions for your Behavior class:

1. Don't create a new Random number generator each time you call the method. You generally only need ONE of them per object.

2. Make ALL your member fields private. ALWAYS. ... or at least for the next year or so.

3. Your randomBehavior() method contains a return statement that returns a String, but the method is declared to return a void.

4. Rather than a big "select" statement, why not have an array of Strings called 'behaviour' and just return the element that corresponds to the random number you create.

ie, something like this:See how much simpler it is? You were on the right lines, but you're overthinking stuff by a country mile.

Think about what you want to do, and then write the simplest code possible to do it.

HIH

Winston
 
Ranch Hand
Posts: 35
2
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I really don't understand what you are trying to do in your behavior class. So I am going to make a few assumptions and try to answer based on those-
1. For loop is there because in your actual code you are running the logic multiple times instead of just once like in your example. If that is not the case then you don't need that loop.
2. From the 4 variables that you are setting in your if loop, only one variable is what you want to return from your method based on whether its null or not. Instead of the method returning void you could just return a String object(for e.g replace 'System.out.println( jiujitsu = "Practicing Jiu Jitsu.");' with 'return "Practicing Jiu Jitsu."'). By doing so, you won't need to set any variables and you'll get only one response object. In case you do need to store the behaviour string in a variable, then why not just use one instead of four? The rest three are anyways always null. In case you need all four, then why not just use an if loop null check and return the variable that is not null in the end? Either ways return something instead of just having a void return type would be my solution based on my understanding.

Hope this helps.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone! This is my second week of doing java and you all are showing me some really good code and making it enjoyable instead of nerve racking. I went back and studied what everyone suggested. Good Points!!
 
and POOF! You're gone! But look, this tiny ad is still here:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic