views:

1232

answers:

3

I just started learning Java at school for cse142, but I've known a bit of programming so am considerably ahead of the class and have been trying my own little projects. Normally if I run into a problem I just work around it because I figure I just haven't learned that yet, but this one bugs me enough to make a whole thread about it.

I want generate the following input/output at console.

Window size? (x, y) : 250, 200

The 250/200 would be user input, so for example, at first you see

Window size? (x, y) :

and you type "250" and hit enter and then you see

Window size? (x, y) : 250,

and after typing "200" and hitting enter you get

Window size? (x, y) : 250, 200

and then it continues on with the program.

As a second question, I'm wondering if it's possible to have a user input an equation. I was thinking about storing what they type into a string, but that would just be text and would need to be transferred into a Java runnable equation.

A: 

Unfortunately, there isn't any support for what you're trying to do in just Java. You could print "Window size? (x,y): " via System.out, and read in text via BufferedReader/System.in but you don't have any control over what appears in the console as a result of user input. If you need that behavior, try and find a CURSES library; I can't recommend any personally.

As for evaluating an equation, you could either parse the string you read in (look into building an abstract syntax tree of some sort). You could also use the string to construct a .java file, and compile and run it by invoking javac and java through the Process class, reading the result out via Process.getInputStream(); which is a pretty nasty solution, but would accept all one line java statements. Such a solution is predicated on the JDK being installed, the JRE alone does not supply javac.

Kevin Montrose
+2  A: 

You can use the Scanner class to get input from the user, like this:

import java.util.Scanner;

public class test
{
    public static void main (String args[])
    {
     Scanner input = new Scanner(System.in);
     System.out.print("Window size? (x, y) : ");
     int xValue = input.nextInt();
     System.out.print("\nWindow size? (x, y) : " + xValue + " , ");
     int yValue = input.nextInt();
     System.out.print("\nWindow size? (x, y) : " + xValue + " , " + yValue);
    }
}

More on scanner here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Josh Leitzel
+1  A: 

I believe that jLine can do what you're looking for. It's BSD licensed, so you're pretty free with how you can use it.

From the jline site:

It is similar in functionality to BSD editline and GNU readline. People familiar with the readline/editline capabilities for modern shells (such as bash and tcsh) will find most of the command editing features of JLine to be familiar.

As for the second part of your question (user-entered equations), take a look at beanshell. For your purposes, it's as easy as:

bsh.Interpreter i = new bsh.Interpreter();
String formula = "2 * 2;"
Object result = i.eval(formula);

You can do a lot more that that with beanshell; the formula can essentially be Java code, and you can put whatever objects you want into the interpreter's "context" for access by the script being evaluated.

Marty Lamb