相关疑难解决方法(0)

在使用next()或nextFoo()之后,Scanner正在跳过nextLine()?

我正在使用这些Scanner方法nextInt()nextLine()阅读输入.

它看起来像这样:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
Run Code Online (Sandbox Code Playgroud)

问题是输入数值后,第一个input.nextLine()被跳过而第二个input.nextLine()被执行,所以我的输出如下所示:

Enter numerical value
3   // This is my input
Enter 1st string    // The program is supposed to stop here and …
Run Code Online (Sandbox Code Playgroud)

java io java.util.scanner

627
推荐指数
17
解决办法
45万
查看次数

为什么缓冲的writer在调用write()方法时不会立即写入

package javaapplication11;

import java.util.Scanner;
import java.io.*;


/**
 *
 * @author jenison-3631
 */
public class JavaApplication11 {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */

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

       // TODO code application logic here
       // File file = new File("/Users/jenison-3631/Desktop/csvv.txt");
        int n,z=1;
        FileWriter writr = new FileWriter("/Users/jenison-3631/Desktop/csvv.txt");
        FileReader fr= new FileReader("/Users/jenison-3631/Desktop/csvv.txt");
        BufferedReader br=new BufferedReader(fr);
        BufferedWriter bw= new BufferedWriter(writr);
        try{
        while(z==1)
        {
            System.out.println("please enter your choice\n1.Add number\n2.Delete number\n3.List all\n4.Search number");
            Scanner s= new …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
1
解决办法
177
查看次数

标签 统计

java ×2

io ×1

java.util.scanner ×1