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 4 of 4

Thread: Void Methods Question

  1. #1
    Junior Member Old Valyria's Avatar
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Void Methods Question

    Okay so beginner programmer here and I've been self-teaching myself Java in preparation for my into class in the Spring.

    I am having trouble getting this program to run. As you can see, I am trying to use the threeLine method to create space between "First Line" and "Second Line" in the main method.

    public class method {
    public static void threeLine() {
    newLine();
    newLine();
    newLine();
    System.out.println("");
    }

    public static void main (String[] args) {
    System.out.println ("First Line."); threeLine();
    System.out.println ("Second Line.");

    }

    }

    Anyway, I am at adding new methods, and here's the code that I have that is not running (I am using a book and the book has the EXACT code but mine isn't running for some reason. I am getting error code "newLine method not defined for the class method"

    Thanks in advance


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Void Methods Question

    Welcome to the forum! Please read this topic to learn how to post code correctly along with other useful info for newcomers.

    What book are you using?

    Style point: In Java, class names begin with capital letters.

    The error message is clear, but I'll add a few words: The threeLine() method calls another method, newLine(). The method newLine() is not recognized by the compiler, so it doesn't know what to do with it. I suspect the newLine() method was covered elsewhere in that book you're reading, or it's part of a custom library that the book has either shown you how to build or download/copy from somewhere. Do a little more looking and see if you can' find some mention of it.

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

    Old Valyria (December 25th, 2013)

  4. #3
    Junior Member Old Valyria's Avatar
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Void Methods Question

    @Greg

    You're spot on. I forgot to declare a new method threeLine(). I've been using "How to Think like a Computer Scientist - Java Programming" by Allen B. Downey

    Once I declared threeLine(), I was able to add newLine() underneath to print what I wanted (which, as you probably guessed, was just space between "First Line" and "Second Line")

    Here's the correct code:

    public class method {
    public static void newLine() {
    System.out.println ("");
    }
    public static void threeLine() {
    newLine();
    newLine();
    newLine();
    }

    public static void main (String[]args) {
    System.out.println ("First Line.");
    threeLine();
    System.out.println ("Second Line.");

    }

    }

    And I apologize I still don't know how to use the code function to put the code in, er, code. Thanks for your help!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Void Methods Question

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Mockito Void Method Question
    By aussiemcgr in forum Java Theory & Questions
    Replies: 0
    Last Post: December 15th, 2013, 03:15 PM
  2. Don't understand void methods, need help!
    By alex067 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2012, 07:02 AM
  3. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM
  4. How to combine these 3 void methods?..
    By TimoElPrimo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 8th, 2011, 02:25 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM