I have this to ask the user a command but I want the input to be written on the same line the user is asked.
public class Controller{
private Scanner in;
public void run(){
String line;
System.out.print("Command > ");
line = in.next();
}
So I get this:
Command >
help
And I want this:
Command > help
Your IDE is lying to you a bit. The current context that you have written will prompt on the same line when you run the .jar, but IntelliJ breaks the input and output between 2 different lines. Rest assured that your prompt is working properly.
Related
I'm trying to take user input and modify it a bit, but ran into an issue when the user inputted multiple lines. To fix this I tried:
public static String getInput() {
Scanner sc = new Scanner(System.in);
String input = "";
System.out.println("Awaiting input...");
if(sc.hasNextLine()) {
System.out.println("Combining Input to One Line...");
while(sc.hasNextLine()) {
//System.out.println(sc.hasNextLine());
//System.out.println("check");
input.concat(sc.nextLine() + " ");
//System.out.println(sc.hasNextLine());
//System.out.println("check2");
//System.out.println("check3");
}
}
sc.close();
return input;
}
It seemed to work until it got to the last line of input, where(after debugging a little) it got stuck trying to read sc.hasNextLine(). However, this is very strange, because I put this exact same code in an online compiler, where it works just fine, except for the fact that the input needs to be beforehand. It doesn't wait until there is any input. I'm not very experienced in Java so I could use some help.
If the infinite loop is your problem, than it can be solved by sending a special EOF marker (Ctrl-D on Unix, usually Ctrl-Z on Windows).
Let's say I have a program that asks for three numbers using scanner, something like this:
Scanner console = new Scanner(System.in);
int[] numbers = new int[3];
System.out.println("Hello!");
for (int i = 0; i<3; i++){
System.out.print("Please enter a number: ");
numbers[i] = console.nextInt();
}
I want to test a program like this by redirecting some input to it, and then redirecting the output to a file.
input.txt
3
4
5
command
java program < input.txt > output.txt
The problem is that the output.txt will look something like this
Hello!
Please enter a number: Please enter a number: Please enter a number:
As opposed to something like this
Hello!
Please enter a number: 3
Please enter a number: 4
Please enter a number: 5
Is there any way to make output.txt look more like the second one? I'm on a Mac, if that changes anything.
I do NOT want to modify the code - I have to run this test for a lot of similar programs.
Why your program doesn't work as expected
In your output, the strings "Please enter a number:" are chained without newlines, because you don't print the newlines in the program. When the program is running in interactive mode, the user enters the newlines.
There are no numbers in the output file, because your program doesn't print them to the standard output. Again, when the program is running in interactive mode, the user enters the numbers, but not the program. In the case of redirected output, the numbers coming from the input file are read by the program, but never printed to the standard output.
The correct way
You should check if the program is running in interactive mode (when the input is read from a TTY).
test/MyApp.java
package test;
import java.util.Scanner;
import java.io.Console;
class MyApp {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Console console = System.console();
int[] numbers = new int[3];
for (int i = 0; i < 3; i++){
if (console != null) {
System.out.print("Please enter a number: ");
}
numbers[i] = scanner.nextInt();
System.out.printf("Number: %d\n", numbers[i]);
}
}
}
Testing
$ printf '%d\n%d\n%d\n' 1 2 3 > file
$ javac -cp . test/MyApp.java
$ java -cp . test/MyApp < file > out
$ cat out
Number: 1
Number: 2
Number: 3
What you may do to have the mixed values for inputs and outputs is :
1) Create your own application, which will call the main method of the application to test (provided all applications have the same package and main class name, or you would have to give this info to your own program, and use reflection to invoke the other program) .
Your application will perform the following steps :
2) Store the regular System InputStream :
InputStream oldInputStream = System.in;
3) Create your own subclass of InputStream (let's call it YourCustomInputStream), and implement the different read methods, to print what was read from System.in to System.out, and also return the value.
e.g :
#Override
public int read(){
int readInt = oldInputStream.read();
System.out.println(readInt);
return readInt;
}
4) Replace the System's input by your own stream :
System.setIn(new YourCustomInputStream());
5) Call the main method of the application to test.
First I wanted to merge stdout and stderr like this:
while read -r num; do
sleep 1
echo ${num}>&2
echo ${num}
done < input.txt | java program
This will fail when you want to redirect the result to a file.
So can you try the next construction:
while read -r num; do
sleep 1
echo ${num}
done < input.txt| tee -a output.txt| java program >> output.txt
I have a program that is supposed to take inputs from the commandline and direct them to standard input and direct the output to standard output. The code supposed to be entered into the commandline is meant to look as follows:
java package.sub.Calc < input-file > output-file
or
echo some inputs to the calculator | java
package.sub.Calc
But I can't seem to get it to work correctly. The goal is to be able to pass a text file into the program or write your math problems right there on the command line and pipe it in.
The program runs correctly for
java -cp . package.sub.Calc
and then having the user type in their problem and hitting enter.
If I have a file named input.txt and want to call it from the command line and have the answers print out in the commandline (program is designed to System.out.println) how would I be entering this information in?
My current code implements a Scanner for System.in. Would I have to use anything else to get this to work? I am new to running anything in the command prompt and can't seem to get it to work.
Any help is greatly appreciated.
My code:
public static void main(String[] args) {
Calc calc = new Calc();
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String input = "";
List<String> strs = new ArrayList<>();
ArrayList<String> tokens;
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty()) {
for (String s : strs) {
sb.append(s);
input = sb.toString();
}
tokens = new ArrayList<>(Arrays.asList(input.split(" ")));
//call calculate and reset
calc.calculate(tokens);
strs.clear();
tokens.clear();
sb.setLength(0);
input = "";
} else if (line.length() == 1) {
strs.add(" ");
strs.add(line);
strs.add(" ");
//manual user exit case
} else if (line.equals("EXIT")){
System.exit(0);
}else {
strs.add(line);
}
}
}
You seem to have 2 problems.
1) You're including the -cp commandline option when you run the program manually, but not when you try and read from the input. You need it in both cases - that's telling java where to find your class file.
2) The way you are handling your input doesn't match the input you're passing in.
You only call calculate when you receive an empty line ( if(line.isEmpty()) ), but something like
echo some inputs to the calculator | java -cp . package.sub.Calc
doesn't ever produce a blank line.
You either need to do:
printf "%s\n\n" "some inputs to the calculator" | java -cp . package.sub.Calc
or change the condition that triggers a calculate (probably by invoking it outside of the while loop).
your input is received in your main method. public static void main (String[] args). The args array is the array which will receive your input parameters IF you have ran your program as java -cp . <main_class_pat> <arg0> <arg1> ...
Make sure your args array has desired length ... and if so, your input file would be at arg[0]
Question:
The commands should be passed in to the program as a file with one instruction per line. The English instructions are in the file commands in english.txt and the Spanish instructions are in the file commands in spanish.txt. You call the program by the passing in the instructions as follows:
java ConsoleRobot < commands_in_english.txt
or
java ConsoleRobot < commands_in_spanish.txt
I don't get what the question is asking? Does it want me to have the commands I enter to go to the english text file or does it want me to have all my commands stored in the english text file?
Here is a link to the full question. I got all of it except the last part. Here is a link to my Java file.
import java.util.Scanner;
public class ConsoleRobot extends SmarterRobot {
public static void main(String [] args) {
World yard = new World();
SmarterRobot ringo = new SmarterRobot();
yard.add(ringo,5,4);
yard.addBeeper(5,9);
yard.addBeeper(4,5);
yard.addBeeper(9,4);
yard.addBeeper(9,5);
Scanner scan = new Scanner(System.in);
System.out.println("Enter a command: | Introduzca un comando:");
String command = scan.nextLine();
command = command.toLowerCase();
while (!command.equals("stop") && !command.equals("detener")) {
if ( command.equals("forward") || command.equals("adelante")) {
System.out.println("How far should the robot move?");
int input = scan.nextInt();
ringo.moveNumOfTimes(input);
} else if ( command.equals("right") || command.equals("derecha"))
ringo.turnRight();
else if ( command.equals("left") || command.equals("izquierda")
ringo.turnLeft();
else if ( command.equals("collect") || command.equals("recoger"))
ringo.pickBeeper();
else if ( command.equals("drop") || command.equals("soltar"))
ringo.putBeeper();
System.out.println("Enter a command: | Introduzca un comando:");
command = scan.nextLine();
}
System.out.println("Finished | Terminado");
}
}
You don't need to do any code to accept the file passed with the < operator. As Majora320 wrote, the < operator renders the file to the standard input. In other wrods, your application will read the commands from the file as if it would have been entered from the keyboard.
The problem is with the scan.nextLine() call. This reads a whole line, and that makes impossible processing commands with parameter, e.g. forward 10 since you read in the whole line, not only the command.
The example below reads first a string (in.next()), and may continue with reading the parameter (in.nextInt()) if a command is expected to have a parameter. But it does not read any parameters for the stop command.
public class Robot {
public static void main(final String[] args) {
final Scanner in = new Scanner(System.in);
while (in.hasNext()) {
final String command=in.next();
if (command.equals("forward")) {
final int distance=in.nextInt();
System.out.println("Forwarding "+distance);
}
if (command.equals("stop")) {
System.out.println("Stopping");
}
}
}
}
The other little problem is that you're keep on reading the input until getting a stop command. This means that command files with no ending stop will not stop your application. It is safer to read until there is something to read, i.e. use while (in.hasNext()) to keep on reading.
Note how indentation and empty lines makes the code more readable, and much easier to follow.
The < operator (At least on the *nix terminal) is for passing the text from a file into the stranded input of a program. For example, grep "hello" < myfile.txt is exactly the same as cat myfile.txt | grep "hello". Basically, < is an abbreviation for running cat file | command. So what java ConsoleRobot < commands_in_english.txt means is pass all the lines in commands_in_english.txt to the stranded input of the ConsoleRobot program. That means you just write the instructions to the file and then run the command.
I'm trying to write a program that reads pairs of words and outputs the number of pairs of identical words. It's assumed an even number of words are inputted. When I run my code, it doesn't output anything. It appears to be continually running. When I press Ctrl-Z after I'm done inputting words, it either returns "0" or nothing at all. Any thoughts on how to make my program run properly? Thanks.
EDIT: it runs fine in the command prompt, but not in Eclipse.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
while (input.hasNext()) {
String string1, string2;
string1 = input.next();
string2 = input.next();
if (string1.equals(string2)) {
++counter;
}
}
System.out.println(counter);
}
You are asking hasNext() once, but then calling next() twice. The second next() can fail if there are no more elements.
It doesn't really work in eclipse for me (nor does in netbeans for what i have read), to test if your code actually works well you should do this:
(In windows) Open a command prompt ( execute ... "cmd" + Enter )
then compile your class or classes with
javac YourClass.java
if no errores it will just let you type a new command with no messages and then
java YourClass
then you can try the Ctrl + z in windows (ctrl + d in linux) which will print the ^Z character and then hit enter.
Hope it helped.