Need a quick C to Java translation...

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Hi guys. I need a C to Java translation. What is:

scanf("%c %d %lx %d\n", &marker, &loadstore, &address, &icount);

in Java? Thanks a bunch!
 

manly

Lifer
Jan 25, 2000
13,086
3,850
136
I don't believe Java has formatted input like scanf.

I would read in a line (DataInputStream of BufferedReader), and then use a regexp to verify it.

J2 SDK 1.4.0 has a standard regexp package, but I've used Jakarta ORO in the past. ORO is considered a very good library.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
yeah, I don't think it does either. it just got formatted output (a la printf style) in 1.4

just read the input into the appropriate data types (though there isn't any point for the hex one, do a conversion using standard Java techniques), and make sure you catch (and handle) the exceptions.
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Thank you for the info.
... I guess I could always tokenize the line right?

Anyhow, another question:: does anyone know how to read a command line argument in Java?
 

manly

Lifer
Jan 25, 2000
13,086
3,850
136
What do you mean read a command-line arg?

Notice the signature of the main method is: void main(String[] args)

args is an array of the command-line arguments. If you're coming from a C background, args in Java does *not* begin with the classname. It begins with the first argument after the classname.

If you're asking how to intelligently handle many command-line arguments, then that's a different story. I think Java leans toward [system] Properties instead of command-line args.

Regular expressions may be a little more advanced than you're expecting at this point, but it is such a fundamental and useful tool in CS that I urge you to consider using it for validate the input line. Manually tokenizing is fine, but matching against a regexp is superior.
 

bo_bear

Senior member
Oct 10, 1999
280
0
0
For command line input, you need to use the InputStreamReader with System.in. Anyway, this syntax is a bit hairy, but below is a simple example.

---------------------------------------------------------------------------------------
import java.io.*;

public class test
{
public static void main (String args[]) throws IOException
{
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));

String input;

System.out.print("Enter something and then press enter: ");

input = stdin.readLine();

System.out.println("You have just entered -> " + input);


}
}