在if语句之后识别扫描仪中的新行

时间:2017-10-26 18:17:49

标签: java android

我对Android有点新手而且我一直遇到扫描仪类的问题,尤其是.newline(),因为我无法在以下代码中使用它java只扫描所有内容没有检查行的文件

package com.cal.omida.calupdater;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
    private TextView textv;
    private Button submitButton;
    private String TAG = "got here";
    private String[] stringsarray = {"empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"};
    private String exp = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        textv = (TextView) findViewById(R.id.mainentry);
        submitButton = (Button) findViewById(R.id.process);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String temp = textv.getText().toString();
                String main = removeTab(temp);
                Scanner scanner = new Scanner(main);
                String[] finalString = scannerclass(scanner);
                Log.d("found", main);
                for (int io = 0; io<11;io++){
                    exp+=finalString[io]+"\n";
                }
                alert(exp,scanner);
            }
        });

    }

    private String[] scannerclass(Scanner scanner){
        scanner.useDelimiter(";");
        int i = 0;
        while (scanner.hasNext()) {
            String text = scanner.next();
            stringsarray[i] = text;
            i++;
        }
        return stringsarray;

    }

    private void alert(String text, final Scanner scanner){
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage(text);
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add to cal",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //todo add to cal function
                        exp=null;
                        if (scanner.hasNextLine()){
                            scanner.nextLine();
                            String[] finalString = scannerclass(scanner);
                            for (int io = 0; io<11;io++){
                                exp+=finalString[io]+"\n";
                            }
                            alert(exp,scanner);
                        }

                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        exp=null;
                    }
                });
        alertDialog.show();
    }

    private String removeTab(String main) {
        String main2 = main.replace("\t",";");
        return main2;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

只是为了澄清我的代码:我有一个由&#34;;&#34;分隔的11部分行。我想扫描线的每个部分将它放入一个数组中使用警告对话框检查数据,如果正面将它添加到我的日历(仍在开发中)并扫描下一行。 我的问题:如果我输入少于11个部件扫描仪继续下一行。有什么方法可以阻止它,所以它只扫描它所在的线而不是继续下一条线?

1 个答案:

答案 0 :(得分:0)

解决方法是使用一个扫描仪获取每一行,然后使用第二个扫描仪分隔每一行。

例如:

String main = /*something*/;
Scanner lineScanner = new Scanner(main); // will scan each line, separated by a newline
while(lineScanner.hasNextLine()){
    Scanner semicolonSeparatedScanner = new Scanner(lineScanner.nextLine());
    /* work from here */
}

请注意,您甚至可以使用String.split进一步简化代码:

while(lineScanner.hasNextLine()){
    String[] semicolonSeparatedText = lineScanner.nextLine().split(";");
}