Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: 1st Java class assignment, and having some difficulties. Little help please.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 1st Java class assignment, and having some difficulties. Little help please.

    So I started my first Java class last week, and Ia m having problems. The question I have in my assignment is;

    "Create an application named TestMethods whose main() methods holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayIt(), displayItTimesTwo(), and displatItPlusOneHundred(). Create each method to perform the task its name implies. Save the application as TestMethods.java."

    So this is what I have so far...


    public class TestMethods
    {
    public static void main(String[] args)
    {
    int myVariable1 = 10;
    int myVariable2 = 27;
    System.out.println(displayIt (myVariable1, myVariable2));
    }
    public static void displayIt(int myVariable1, int myVariable2)
    {

    System.out.println("My two variables are " + myVariable1 + " and " + myVariable2);
    }


    }





    I keep getting "void type not allowed here' on line 16 which is bold. What am I doing wrong here? This is my first assignment, and it has been frustrating. I know it's an easy question, but I cannot get it in to code for the very first part. Any help is greatly appreciated.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    Hello, flpanthers1. The reason you are getting this error is because the System.out.println method has a parameter of type 'String', and therefore a method declared as type void cannot be invoked. Try changing the method 'displayIt' so that it is declared as a String. (this will require a return statement)

    Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.

    You can learn more about primitive data types here:
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You can learn more about methods and parameters here:
    http://download.oracle.com/javase/tu...O/methods.html
    Last edited by Karl; May 25th, 2011 at 10:10 AM.

  3. The Following User Says Thank You to Karl For This Useful Post:

    JavaPF (May 26th, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    Instead of passing the call to displayIt to println, call displayIt directly. The displayIt method calls println to do the printing.

    Don't work about saving a few bytes of memory. Using int can be more efficient and you'll likely have fewer headaches when you change the values in the numbers to be too large for bytes.
    Last edited by Norm; May 25th, 2011 at 10:28 AM.

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    Ok, so I changed this up a little, but I cannot figure out why I am getting this...


    My first variable is 10
    10
    My second variable is 27
    27

    Process completed.


    I realize it has something to do with the return I put in, but I really stumbled across the solution. to be honest, when I saw "Process Completed" I was happy it had no errors. But of course you see the extra '10' and '27'. not sure why this is either. I appreciate your patience.


    public class TestMethods
    {
    public static void main(String[] args)
    {
    int myVariable1 = 10;
    int myVariable2 = 27;
    System.out.println(displayIt (myVariable1, myVariable2));
    System.out.println(displayIt2 (myVariable1, myVariable2));
    }
    public static int displayIt(int myVariable1, int myVariable2)
    {
    System.out.println("My first variable is " + myVariable1);
    int newAmount;
    newAmount = myVariable1;
    return newAmount;
    }
    public static int displayIt2(int myVariable1, int myVariable2)
    {
    System.out.println("My second variable is " + myVariable2);
    int newAmount;
    newAmount = myVariable2;
    return newAmount;
    }

    }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    You code does double printing. The displayIt methods print things AND return data that is printed by the caller. You need to figure where you want the data printed and only print it there. Only do the print in one place! If you return a value to be printed, do NOT print it before returning it.

  7. #6
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    I see what you mean, but every time I remove something, or add it I still get errors. It's making me nuts, and I know this is simple. I see the two println's in there, but I delete one, and I get errors.... Delete the other, and it still does not work. This is what I have now with the result at the bottom. I am only supposed to have the one displayIt method, so I had to delete the displayIt2


    public class TestMethods
    {
    public static void main(String[] args)
    {
    int myVariable1 = 10;
    int myVariable2 = 27;
    System.out.println(displayIt (myVariable1, myVariable2));
    }
    public static int displayIt(int myVariable1, int myVariable2)
    {
    System.out.println("My first variable is " + myVariable1);
    System.out.println("My second variable is " + myVariable2);
    int newAmount;
    newAmount = myVariable2;
    return newAmount;
    }
    }

    Result:

    My first variable is 10
    My second variable is 27
    27

    Process completed.

  8. #7
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    Quote Originally Posted by Karl View Post
    Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.
    With todays modern hardware (and the excessive amounts of RAM available) you won't be saving much memory by doing that. I suggest staying away from bytes and shorts (unless you have a very good reason to use them) and only use ints and longs to represent integers.

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    Quote Originally Posted by Karl View Post
    Also, while using variables that don't have a value over 128, or below -127 it is recommend to use the 'byte' keyword instead of the 'int' keyword. This is to save memory as an integer is a 32-bit integer, and a byte is an 8-bit integer.
    This is only really relevant where very large arrays are involved - and on retrieval from a byte array, all bytes are sign-extended to integers and any manipulation is done with these integer values (see VM Spec: baload).

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    I still get errors.
    Step thru your code line by line and play computer. See when each println is called and with what values. When a method is called as an argument to another method, the called method executes first and then can return a value that will be used by the calling method.

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    You might want to seek clarification but I interpret the instructions as each method only accepts a single parameter, the value to be displayed. So you create 2 variables and then call displayIt with var 1 and then call displayIt with var 2.

  12. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: 1st Java class assignment, and having some difficulties. Little help please.

    The requirements imply that the printing should be done inside the displayIt.. methods (as the method names also suggest), so these methods do not need to return a value - after all, the only value they have is the one passed to them, so there's no point returning it. In other words, declare the displayIt.. methods as 'void' and just call them directly, don't call them from inside a System.out.println(..) call.

Similar Threads

  1. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  2. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 12:35 AM
  3. [SOLVED] OOP java assignment
    By enkei in forum Object Oriented Programming
    Replies: 1
    Last Post: April 27th, 2011, 11:16 AM
  4. Java Assignment Help
    By welsh_rocker in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 12th, 2011, 06:02 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM