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
Code :
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?
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.
Code :
public void getInput()
{
// your getInput code
}
public static void main(String[] args)
{
new Example().getInput();
}