I am trying to get the user to enter their name and have it be printed out on java using Sublime Text 3. After the user enters their name when a message prompts them for it, their name is supposed to be printed out but it justs skips over to a new blank line. It works on online compilers but not on Sublime. How do I get user console input to work on Java Sublime Text 3?
import java.util.Scanner;
public class SocSecProcessor {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
String name = input.nextLine();
input.close();
System.out.println(name);
}
}
You have to launch execution in a separate console, not the sublime text pseudo console.
The solution is to launch with START command in a batch file .bat
for example:
START CMD /K %RELATIVE_PATH%\jdk\bin\java -cp %~dp1 %~n1
where %1 is the file class with main method. Go to the batch syntax documentation if necessary... Your build system in sublime must launch a batch file with this kind of
instruction.
Related
Usecase:
I want to mask input text(mainly password) in run.bat file, i am using java as programming language,overall my major objective is that whenever user provide input for password in run.batch it should be encrypted,is it possible to do so?
Here is my run.batch file :
#echo OFF
setlocal DISABLEDELAYEDEXPANSION
java -jar "stack.jar" -username "stackoverflow" -password "qwerty#567" -configpath "config.ini"
pause
Here i want to mask qwerty#567 which is password given by user end
Here is something that i tried but it works only for console
import java.io.Console;
public class Main {
public static void main(String[] args) {
Console console = System.console();
char[] passwordArray = console.readPassword("Enter your password: ");
console.printf("Password is: %s%n", new String(passwordArray));
}
}
The password can be an argument to your batch file: run.bat.
Modified batch file (from your question)
#echo OFF
setlocal DISABLEDELAYEDEXPANSION
java -jar "stack.jar" -username "stackoverflow" -password %1 -configpath "config.ini"
pause
And launch your batch file as:
run.bat my_password
Now the password does not appear in the batch file.
In windows 10, If you take the input directly as a variable in batch, using Set /P, you can change the Color code to black on black for the input using the ANSI Color code <esc>[30m (with <esc> replaced by the Ansi escape character)
Use <esc>[0m after taking input to reset the color.
I have compiled a jar file. Here is the code:
package javaProject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Enter something >>> ");
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
scanner.close();
}
}
Here is my Apple Script script:
do shell script "java -jar /Users/Harry/Desktop/appleScript.app/Contents/javaProject.jar"
Nothing is printed in the console, and the scanner object doesn't ask for my input at any point, but the JFrame is created. Can anyone explain why this is the case? Thanks.
do shell script doesn't run with/in a console, the result (or error) is just returned to the script. If you are wanting to interact or view progress, the script can be run in something that does provide a console, such as the Terminal (which is scriptable). Note that a new window/tab will be created each time you run a script, unless you specify something different - for example, the following script will always use the first tab of the first window:
tell application "Terminal"
activate
try
do script "echo 'This is a test.' " in tab 1 of window 1
on error errmess -- no window
log errmess
do script "echo 'This is a test.' "
end try
end tell
I just started learning JAVA and Sublime Text 3 was proposed to me as a great compiler for JAVA code. I downloaded it, started programming and set my build system as JavaC. I wanted to create a quick program adding two numbers given by the user and displaying the result but nothing comes up in the "build" section. Do you have any idea to make that work ?
Here is the code I wanna try:
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a,b;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
a=input.nextInt();
System.out.println("Enter a number:");
b=input.nextInt();
System.out.println("sum=" + (a+b));
}
}
For future references on how this windows command line or command prompt in windows works.
First check if your computer have the JRE (required to run a Java program) and JDK ( required compile and run Java programs) already install to verify this input the text "java -version" and javac -version into the Command Line. If the Javac is already intalls, however, doesn't show in the command line then you need to follow this sites on how to set up the path depending on your OS.
Dear fellow java enthusiast,
I would like to make my java program into a executable .jar file with the help of eclipse. I exported into a .jar file but when I double click it it does not seem to work? I think i am missing something but after some time trying to find how to do it i cant find it.
Any advice?
import java.util.Scanner;
public class GravityMoon {
public static void main(String[] args) {
System.out.println("Please enter your earthweight in kilograms below:");
Scanner earthweight = new Scanner(System.in);
// Repeat until next item is a double
while (!earthweight.hasNextDouble())
{
// Read and discard offending non-double input
earthweight.next();
// Re-prompt
System.out.print("Please enter your earthweight "
+ "in numerical kilograms: ");
}
double moonweight;
// Converts earthweight in moonweight 17%
moonweight = earthweight.nextDouble() * 0.17;
// convert to whole numbers
//closes keyboard input
earthweight.close();
//prints out your moonweight in a decimal number
System.out.println("Your moonweight is " + Math.round(moonweight * 100d) /100d
+ " kilograms.");
}}
as you did not explain the exact problem, I might only give hints:
Export as Executable Jar
In Eclipse, there is the option to export into a jar file and another option to export into an executable jar file (see http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm).
(I guess you are using windows) Try to execute the jar file from the console (something like java -jar ).
Double klicking a jar file in windows does not open a terminal window.
I've just started working with Java, and I've been trying to get the console input to work properly. Here's the code:
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
This should just take input once and the print the input, like this:
Write a word:
DEBUG 1
Hello //My input
DEBUG 2
Hello //Output
But this is what happens:
Write a word:
Hello //My input
DEBUG 1
//Waiting for new input
DEBUG 2
Hello //The first input
So, it seems to me that it's somehow takes the input at the line Scanner keyboard = new Scanner(System.in); and then put it in my variable str. I use gcj to compile with the following commands to compile and run:
javac hello_world.java
java hello_world
EDIT: I've tried running the program on another computer now, using Ubuntu 10.04 64-bit, instead of Ubuntu 10.04 32-bit as before. I ran it the same way and did not recompile, and the program works fine.
Any ideas why it's behaving like this?
There could be a flushing/buffering issue that causes DEBUG 1 to be output later than you expect. I.e., println("DEBUG 1") could be executed before keyboard.nextLine(), but for some reason the output gets held up on the way, and not output until your input is read.
But I take it that you mean that you actually have to enter two newlines, one after Hello and one at the “Waiting for new input” line. If so, this is more difficult to explain. Perhaps a line termination incompatability issue.
Compiling and executing using the standard JDK gives the behavior you expect.
I checked your program in eclipse running on windows using Oracle JDK 6, I got the expected result.
public static void main(String[] args) {
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
String str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
}
Result :
Write a word:
DEBUG 1
hello
DEBUG 2
hello
OpenJDK is much similar to Oracle JDk and it is available for many distros from their package manager. Can you check with openJDK instead of gcj.