www.webdeveloper.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32

Thread: help with my simple method :( please

  1. #16
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    The ArrayList (Or List) object would be helpful. It is also in java.util.

    Ues it as a dynamic array, that is, you can dynamically add and remove Objects to and from it.

    Regards.

  2. #17
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Note, techically, a function can only return a single value. All we are doing here is grouping several values into one and sending that Object back to the calling environment.

    Regards.

  3. #18
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Well yeah, it wouldn't make much sense if a function actually returned multiple values. There would be ambiguity. It would rain muffins, we'd wear hats on our feet, and hamburgers would eat people.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  4. #19
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    I still don't understand why my function isn't being called. :|

    I've been trying to debug it for a few hours already. lol. Gosh, I suck at Java.

    "Everything in a web browser."

  5. #20
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    protected - can be access from any object in the same package.
    left blank - similar to public. You can look up the difference.
    If it is left blank then it can be accessed by any class within the same package. If it is protected then it is accessible only within its own class and any class that extends it.

    We agree on the point that one should have a good reason for declaring anything static.
    If you want a simple rule to follow: if a method does not use any instance variable then it should be static.

    public int getAverage(int a, int b, int c, int d, int e, int f)
    {
    return ((a+b+c+d+e) / f);
    }
    It doesn't make much sense to me to include the F parameter. The number (in this case) *must* be 5, otherwise the calculation will be wrong.

    Code:
    public int getAverage(int a, int b, int c, int d, int e)
    {
        return (a+b+c+d+e) / 5;
    }
    If you have a static method then it will complain if you call it relative to the object rather than the class. "not called in a static way" it whines.
    This is a good thing. It helps to keep what your program is doing more clear. I would think it would speed up execution time too (granting not very much).

    public String RepeatWord(String iWord, int iRepeat)
    {
    if (iRepeat < 1) {
    System.out.println("I cannot repeat the word less than 1 time. That's physically impossible.");
    } else {
    for (int count=1; iRepeat <= count; count++) {
    do {
    count++;
    System.out.println (iWord);
    } while (iRepeat <= count);
    }
    }

    }
    First thing that sticks out is that you never actually return anything. The method should either return the repeated string and not print anything, or print the repeated string and return void.

    Second thing, you are incrementing count in your loop body as well, which you shouldn't. In fact I think the entire do-while construct should be removed. As it is now it will leave you in an infinite loop.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  6. #21
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    ahhhhh...

    How would I be able to make it print out a string depending on the input # that they put in??

    If they put in 5 for input.

    It'll print out....

    foo
    foo
    foo
    foo
    foo

    I'm so confused with the do-while loop.

    "Everything in a web browser."

  7. #22
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Thanks for that, Jeff.

    It would be even more logical to simply use an array.
    Code:
    public int getAverage(int [] arr)
    {
        int total = 0;
        for (int i = 0; i < arr.length; i++)
            total += arr[i];
        return (total / arr.length);
    }
    ...
    int [] avg = {1, 2, 3, 4, 5};
    System.out.println(Integer.toString(
                       getAverage(avg)));
    Regards.

  8. #23
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Do I have to construct a new object just to use my own function/method ???

    It keeps on saying a problem with System.out.println(repeatWord(iWord));

    "Everything in a web browser."

  9. #24
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    If you want a simple rule to follow: if a method does not use any instance variable then it should be static.
    I like that one. Of course you then need to justify why that method was put into that class if the class itself is not some sort of utility class.

  10. #25
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Originally posted by BuezaWebDev
    Do I have to construct a new object just to use my own function/method ???

    It keeps on saying a problem with System.out.println(repeatWord(iWord));
    I think your main problem is you're still not thinking in objects. (BTW, your RepeatWord() should be named repeatWord() because the convention is to capitalize classes and camel case almost everything else.)

    Your method is being called but it's not returning a String as promised. To do that you'd have to do something like.
    Code:
    public String repeatWord(String iWord, int iRepeat)
    {
      StringBuffer sb = new StringBuffer();
    
      if (iRepeat < 1) {
        sb.append("I cannot repeat the word less than 1 time. "
                  +"That's physically impossible.");
      } else {
        for (int count=1; iRepeat <= count; count++) {
          sb.append(iWord + " ");
        }
      }
      return sb;
    }
    Your IDE is REALLY letting you down. Eclipse would never let you get away with not returning what you claimed you'd return.

  11. #26
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Originally posted by ray326
    I think your main problem is you're still not thinking in objects. (BTW, your RepeatWord() should be named repeatWord() because the convention is to capitalize classes and camel case almost everything else.)

    Your method is being called but it's not returning a String as promised. To do that you'd have to do something like.
    Code:
    public String repeatWord(String iWord, int iRepeat)
    {
      StringBuffer sb = new StringBuffer();
    
      if (iRepeat < 1) {
        sb.append("I cannot repeat the word less than 1 time. "
                  +"That's physically impossible.");
      } else {
        for (int count=1; iRepeat <= count; count++) {
          sb.append(iWord + " ");
        }
      }
      return sb;
    }
    Your IDE is REALLY letting you down. Eclipse would never let you get away with not returning what you claimed you'd return.
    I'm currently using JGRASP. NetBeans is weird--I can't even save the file to somewhere I want.

    "Everything in a web browser."

  12. #27
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Code:
    // Program will ask user to input a word
    // Program will ask user to input a number of times to repeat the word
    
    import cs1.Keyboard;
    
    public class booya
    {
    	public static String repeatWord(String iWord, int iRepeat)
    	{
    	  StringBuffer sb = new StringBuffer();
       
    	  if (iRepeat < 1) {
    	    sb.append("I cannot repeat the word less than 1 time. "
    	              +"That's physically impossible.");
    	  } else {
    	    for (int count=1; iRepeat <= count; count++) {
    	      sb.append(iWord + " ");
    	    }
    	  }
    	  return sb; // NetBeans keeps red underlining this line.
    	}
    	public static void main(String[] args)
    	{
    		System.out.println("Please enter a word: ");
    		String iWord = Keyboard.readString();
    		System.out.println("Please enter how many times you wish to repeat it: ");
    		int iRepeat = Keyboard.readInt();
    
    		System.out.println(repeatWord(iWord, iRepeat)); 
    
    	}
    
    
    
    }
    Hrmm...NetBeans keeps underlining the return statement. :|

    "Everything in a web browser."

  13. #28
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    It's probably because the return value for the method is a String but you are returning a StringBuffer. Also, I don't know if you can use System.out.println to print a StringBuffer. You may find you have to create a String from the StringBuffer in order to print it. Or if you want to return a string,
    Code:
    return new String(sb);
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  14. #29
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    REVISED.

    Now it will just ask user to input the number of times to repeat the word "Alarm".

    Code:
    import cs1.Keyboard;
    
    public class Alarm1
    {
    	public int repeatWord(int x)
    	{
    		String output = "Alarm";
    			for (int count=1; x <= count; count++) {
    				System.out.println(output);
    			}
    	return output; // ERROR HERE?	
    	}
    	public static void main (String[] args)
    	{
    		int input;
    		System.out.println("Yo, how many times do you want to repeat the word ALARM?");
    		input = Keyboard.readInt();
    
    		if (input < 1) {
    			System.out.println("Input is below 1. Error.");
    		} else {
    			System.out.println(repeatWord(input)); // ERROR HERE?
    		}
    		
    	}	
    }
    However, it keeps giving me an error on my last SYSTEM.OUT.PRINTLN and the RETURN line (incompatible types).

    Please help.

    "Everything in a web browser."

  15. #30
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Code:
    public int repeatWord(int x)
    	{
    		String output = "Alarm";
    			for (int count=1; x <= count; count++) {
    				System.out.println(output);
    			}
    	return output; // ERROR HERE?	
    	}
    Your return value is int, but you're returning a String. Of course it's going to complain. On another note, your loop looks pretty infinite to me, unless you call the function with x=0. If you just want a function to print the word those multiple times, make it with return value void, and return nothing:
    Code:
    public static void repeatWord(int x) {
      for(int i=0; i<x; i++) {
        System.out.println("Alarm");
      }
      return; //note this return statement is optional
    }
    Then in your main:
    Code:
    public static void main (String[] args)
    	{
    		int input;
    		System.out.println("Yo, how many times do you want to repeat the word ALARM?");
    		input = Keyboard.readInt();
    
    		if (input < 1) {
    			System.out.println("Input is below 1. Error.");
    		} else {
    			repeatWord(input);
    		}
    		
    	}
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles