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

Thread: Newbie Programming problem

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie Programming problem

    Hi, I'm trying to write a program that takes in user input and only terminates when a certain word is entered.
    I so far have this


    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
     
    public class Example
    {
     
        private void getInput(String line)
        {
     
           BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Enter String: ");
     
               try
                    {
                            line = reader.readLine();
                     }
                       catch(IOException e)
                       {
                            System.out.println ( "Error" ) ; 
                        } 
     
     
      while(true)
      { if ("terminate".equals(line))
               {
                  break;
              }
     
         else
            {
                System.out.println(line);
               line = reader.readLine();
            }
        }
     
    }  
     
     
    private static void main(String[] args)
    {
      new Example().getInput(String line);
    }
    }

    When I compile this it tells me that it expects ')' in the last method. Anyone know how I can fix this?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Newbie Programming problem

    You don't need to put the full method signature in order to call a function, just the parameters. Also, since line is going to be used locally in your getInput() method, I would just declare the method to take no arguments and call it. You will also run into run-time errors because you declared the main method private, but it should be public. You will have a similar problem (though, this one will register as a compile error) becuase getInput() should also be declared public.

    public void getInput()
    {
       // your getInput code
    }
     
    public static void main(String[] args)
    {
         new Example().getInput();
    }

Similar Threads

  1. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM
  2. newbie here
    By salpoe in forum Member Introductions
    Replies: 0
    Last Post: January 15th, 2010, 01:05 AM
  3. newbie needs help with for loop
    By b00036962 in forum Loops & Control Statements
    Replies: 2
    Last Post: January 7th, 2010, 04:57 AM
  4. Newbie...
    By jchan in forum Member Introductions
    Replies: 2
    Last Post: September 28th, 2009, 02:39 AM
  5. I'm a newbie
    By r12ki in forum Member Introductions
    Replies: 2
    Last Post: June 1st, 2009, 06:38 AM