hasNextInt不会返回false并退出循环

时间:2018-10-04 22:02:12

标签: java input

我正在尝试将用户输入放入数组,但是hasNextInt()方法不会返回false并停止输入。

    package com.example.nisarga.testapplication;

import android.app.IntentService;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;


public class BlueToothService extends IntentService
{

    private final String TAG="TestTag";
    public BlueToothService()
    {
        super("BlueToothService");
    }

    @Override
    protected void onHandleIntent( Intent intent)
    {

    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(),"Your device does not support bluetooth",Toast.LENGTH_SHORT).show();
            Log.d(TAG,"Device does not support bluetooth");
            stopSelf();
        }
        else
        {
            if (!mBluetoothAdapter.isEnabled())
            {
                Toast.makeText(getApplicationContext(), "Please turn on the bluetooth", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Bluetooth is not turned on");
                stopSelf();
            }
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {
            // There are paired devices. Get the name and address of each paired device.
            for (BluetoothDevice device : pairedDevices) {
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
            }
        }

        AcceptThread acceptThread=new AcceptThread();
        acceptThread.start();


    }

    private class AcceptThread extends Thread {
        private final BluetoothServerSocket mmServerSocket;


        public AcceptThread() {
            // Use a temporary object that is later assigned to mmServerSocket
            // because mmServerSocket is final.
            BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
            BluetoothServerSocket tmp = null;
            try {
                // MY_UUID is the app's UUID string, also used by the client code.
                tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Samaya", UUID.fromString("9fb4c0be-2229-43e4-be74-e53860a26420"));
                Log.d(TAG, "Accepting the requests");
            } catch (IOException e) {
                Log.e(TAG, "Socket's listen() method failed", e);
            }
            mmServerSocket = tmp;
        }

        public void run() {
            BluetoothSocket socket = null;
            // Keep listening until exception occurs or a socket is returned.
            while (true) {
                try {
                    socket = mmServerSocket.accept();
                    Log.d(TAG, socket+"");

                } catch (IOException e) {
                    Log.e(TAG, "Socket's accept() method failed", e);
                    break;
                }

                if (socket != null) {
                    // A connection was accepted. Perform work associated with
                    // the connection in a separate thread.
                    Log.d(TAG, "Closing the server socket");
                    ConnectedThread connectedThread=new ConnectedThread(socket);
                    connectedThread.start();
                    try
                    {
                        mmServerSocket.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }

        // Closes the connect socket and causes the thread to finish.
        public void cancel() {
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "Could not close the connect socket", e);
            }
        }
    }



        private class ConnectedThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final InputStream mmInStream;

            private byte[] mmBuffer; // mmBuffer store for the stream

            public ConnectedThread(BluetoothSocket socket) {
                Log.d(TAG, "Connected");
                mmSocket = socket;
                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the input and output streams; using temp objects because
                // member streams are final.
                try {
                    tmpIn = socket.getInputStream();
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred when creating input stream", e);
                }
                try {
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred when creating output stream", e);
                }

                mmInStream = tmpIn;

            }

            public void run() {
                Log.d(TAG, "Waiting for inout");
                mmBuffer = new byte[1024];
                int numBytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs.
                while (true) {
                    try {
                        // Read from the InputStream.
                        numBytes = mmInStream.read(mmBuffer);
                        Log.d(TAG,mmBuffer.toString());


                    } catch (IOException e) {
                        Log.d(TAG, "Input stream was disconnected", e);
                        break;
                    }
                }
            }

            // Call this method from the main activity to shut down the connection.
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) {
                    Log.e(TAG, "Could not close the connect socket", e);
                }
            }

        }
}

1 个答案:

答案 0 :(得分:0)

Scanner.hasNextInt()在其缓冲区中遇到非整数字符时将返回false。 但是,在阅读提示时,它可能会删除空格。因此Space+EnterEnter很可能不会停止循环。但是任何其他角色都会。

由于您要输入任意数量的整数,因此必须指导用户完成输入的内容。实际上,如果您正在编写控制台应用程序,则最好始终解释为什么程序正在等待输入的好主意。

任何非整数都将停止循环条件。在这种情况下,语法将按原样工作,用户只需要一些说明即可:

System.out.println("Please enter the target");
int target = in.nextInt();
System.out.println("Enter weights. Type 'X' to stop");
while(in.hasNextInt()) {