Netbeans data entry
-
I'm beginning with java and I'm using NetBeans IDE 6.1 to edit, build and run my first sourcefiles and projects. The problems is when I need to enter data to my program. For example, look at the following code:
package formattedprint;
import java.util.*;public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);System.out.println("Enter an integer number:\\n"); int intNum = in.nextInt(); System.out.println("Enter a string:\\n"); String string = in.nextLine(); System.out.println("------------------------------"); System.out.printf("Integer number: %d%n", intNum); System.out.printf("Cadena: %s%n", string); System.out.println("------------------------------"); }
}
When I run this code, there is not an input window. Only I can enter data in the "output window", and it seems to work in a bad way: When the program ask for the integer number and I enter 1 (for example), the program finish without ask for the string. This is the output window:
init:
deps-jar:
Compiling 1 source file to /home/adrian/NetBeansProjects/FormattedPrint/build/classes
compile:
run:
Enter an integer number:Enter a string:
Integer number: 1
Cadena:1
BUILD SUCCESSFUL (total time: 6 seconds)I think, I'm doing something wrong. (May be I should open an input window, but I didn't find it.) Please, can somebody help me?
[Adrián Córdoba]
-
I'm beginning with java and I'm using NetBeans IDE 6.1 to edit, build and run my first sourcefiles and projects. The problems is when I need to enter data to my program. For example, look at the following code:
package formattedprint;
import java.util.*;public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);System.out.println("Enter an integer number:\\n"); int intNum = in.nextInt(); System.out.println("Enter a string:\\n"); String string = in.nextLine(); System.out.println("------------------------------"); System.out.printf("Integer number: %d%n", intNum); System.out.printf("Cadena: %s%n", string); System.out.println("------------------------------"); }
}
When I run this code, there is not an input window. Only I can enter data in the "output window", and it seems to work in a bad way: When the program ask for the integer number and I enter 1 (for example), the program finish without ask for the string. This is the output window:
init:
deps-jar:
Compiling 1 source file to /home/adrian/NetBeansProjects/FormattedPrint/build/classes
compile:
run:
Enter an integer number:Enter a string:
Integer number: 1
Cadena:1
BUILD SUCCESSFUL (total time: 6 seconds)I think, I'm doing something wrong. (May be I should open an input window, but I didn't find it.) Please, can somebody help me?
[Adrián Córdoba]
Hi, yep that was kinda weird or was just done for file reading. :S btw i dunno why are you using printf if you have the easier methods which are println and print :P however its ur decision but i find this is working XD anyways you can use this code: public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter an integer number:\n"); int intNum = in.nextInt(); System.out.println("Enter a string:\n"); //This will only read the first string aka the first name if u wish String string1 = in.next(); //This is for the last name if you wish String string2 = in.next(); System.out.println("------------------------------"); System.out.println("Integer number: " + intNum); System.out.println("Cadena: " + string1 + " " + string2); System.out.println("------------------------------"); } } The output: init: deps-jar: compile: run: Enter an integer number: 1 Enter a string: Jassim Makki ------------------------------ Integer number: 1 Cadena: Jassim Makki ------------------------------ BUILD SUCCESSFUL (total time: 6 seconds)
BlaCk WolViX
-
Hi, yep that was kinda weird or was just done for file reading. :S btw i dunno why are you using printf if you have the easier methods which are println and print :P however its ur decision but i find this is working XD anyways you can use this code: public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter an integer number:\n"); int intNum = in.nextInt(); System.out.println("Enter a string:\n"); //This will only read the first string aka the first name if u wish String string1 = in.next(); //This is for the last name if you wish String string2 = in.next(); System.out.println("------------------------------"); System.out.println("Integer number: " + intNum); System.out.println("Cadena: " + string1 + " " + string2); System.out.println("------------------------------"); } } The output: init: deps-jar: compile: run: Enter an integer number: 1 Enter a string: Jassim Makki ------------------------------ Integer number: 1 Cadena: Jassim Makki ------------------------------ BUILD SUCCESSFUL (total time: 6 seconds)
BlaCk WolViX
-
Thank you. I'll try your recommendations. P.S.: I was using printf method because it was recommended in the practice of the book I studying with (Core Java 2).
[Adrián Córdoba]
no problemo anytime buddy ;)
BlaCk WolViX