English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

System.out.print ("Enter name: ");
sName = keyboard.nextLine();

System.out.print ("Enter hours worked: ");
shHours = keyboard.nextShort();

I have this piece of code in my program! Sname is declared as a string. When I run the program it displays:

Enter name: Enter hours worked:

It does not allow me to enter the name. The program skips it and jumps to Enter hours worked: . Does anyone have any suggestions!

2007-02-20 08:53:41 · 1 answers · asked by jhn_grz 3 in Computers & Internet Programming & Design

1 answers

It's because the nextLine() method does not allow you to enter the name... you need some other method like readLine() or readString() I'm not sure if this methods exist in your keyboard class you have to check that.

Another way is to use this
import java.io.*;

public class X{
private static DataInputStream keyboard = new DataInputStream (System.in); //stream to read from the keyboard.

public static void main(String args[][]){

try{
System.out.print ("Enter name: ");
sName = keyboard.readLine(); // call for method to read a line returns a string.
System.out.print ("Enter hours worked: ");
shHours = keyboard.readShort();
}
catch(IOException e) {
System.out.println("Error read line");
}}}

That should work. Hope I helped

Good luck

2007-02-20 14:00:40 · answer #1 · answered by jessieg14 3 · 0 0

fedest.com, questions and answers