Results 1 to 13 of 13
Thread: Read from command line
- 02-23-2011, 03:57 AM #1
Read from command line
I am trying to build a simple program to read input from the command line, so far I have in my main method:
public static void main(String[] args) {
String input = null;
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
input = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Your name is "+input);
}
For some reason, its not waiting on my input, its printing out "Enter your name: null"
I'm not sure what's going on, I thought System.out.print allowed me to print to the screen, and then I can use "input = br.readLine" to set input variable to whatever they enter.
- 02-23-2011, 04:01 AM #2
Are you sure?
Have you saved your code?
Have you compiled the latest version of your code?
- 02-23-2011, 04:05 AM #3
positive.
I compiled it and ran it, and it says "Enter your name: null" and then goes to a new line with a blinking cursor.
- 02-23-2011, 04:07 AM #4
Hmmm.
Clutching at straws: delete the .class file and compile again.
Provide details of how you are compiling and running the program.
- 02-23-2011, 04:12 AM #5
Im using Netbeans to create a .jar file, and then running it from the command line:
java -jar "C:\Users\MyCPU\Desktop\ProjectFolder\MyProgram\di st\CommandLineApp.jar"
- 02-23-2011, 04:19 AM #6
Compiling your code and running from the command line (no jar) works fine.
- 02-23-2011, 04:25 AM #7
hmm..
I get a NoClassDefFoundError when I try to just put the code that I gave you into a "Main.java" file and then do a javac Main.java (compiles fine), and try to run it " java Main"
- 02-23-2011, 04:29 AM #8
Post your FULL code.
- 02-23-2011, 04:31 AM #9
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/**
*
* @author Steve
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String input = null;
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
input = br.readLine();
System.out.println("Your name is "+input);
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
}
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getReso urceAsStream(url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
}
- 02-23-2011, 04:37 AM #10
I just stuffed your code (without all the extra crud) into a jar file and it ran fine for me. So I can only assume that once again you are not using the code you think your are in the jar file. Or netbeans is doing something hinky when it creates the jar file.
- 02-23-2011, 04:38 AM #11
- 02-23-2011, 04:42 AM #12
weird...im not sure what else I can do.
- 02-23-2011, 04:44 AM #13
Similar Threads
-
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 03:09 PM -
can i run line by line command in netbean?
By choconlongxu in forum NetBeansReplies: 1Last Post: 07-19-2010, 08:41 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks