While循环程序无法正常运行

时间:2019-11-05 19:35:57

标签: java eclipse loops

我的while循环似乎有一个我找不到的问题。当我运行程序时,它表明它可以运行,但是什么也没有发生。帮助。

public class DatabaseHandler extends SQLiteOpenHelper {
    // Database Version
    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "spinnerExample";
    private final Context myContext;
    private SQLiteDatabase myDataBase;
    // Database Name
    // Labels table name
    public static final String TABLE_LABELS = "labels"; //<<<< Made public
    public static final String TABLE_LABELS1= "labels1";
    public static final String TABLE_LABELS2= "labels2";
    // Labels Table Columns names
    public static final String KEY_ID4 = "input_label";
    public static final String KEY_ID12 = "id2";           //<<<< Made public
    public static final String KEY_ID = "id";
    public static final String KEY_99 = "sno";           //<<<< Made public//<<<< Made public
    public static final String KEY_NAME = "name";       //<<<< made public
    public static final String KEY_ID1 = "id1";           //<<<< Made public
    public static final String KEY_NAME1 = "name1";
    public static final String KEY_1 = "number";           //<<<< Made public
    public static final String KEY_2 = "outletname";       //<<<< made public
    public static final String KEY_3 = "sunday";           //<<<< Made public
    public static final String KEY_4 = "monday";
    public static final String KEY_5 = "tuesday";
    public static final String KEY_6 = "wednesday";
    public static final String KEY_7 = "thursday";
    public static final String KEY_8 = "saturday";
    public static final String KEY_9 = "closed";
    public static final String KEY_10 = "calling";
    public static final String KEY_11 = "id3";
    public static final String ROUTE= "route";

    public DatabaseHandler(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        myDataBase = this.getWritableDatabase();
        this.myContext = context;
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        //boolean dbExist = checkDataBase();
        // Category table create query
        String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("+ KEY_99 + " INTEGER,"
                + ROUTE + " TEXT," + KEY_ID + " TEXT," + KEY_NAME + " TEXT)";
        String CREATE_CATEGORIES_TABLE1 = "CREATE TABLE " + TABLE_LABELS1 + "("
                + KEY_ID1+ " TEXT," + KEY_NAME1+ " TEXT)";
        String CREATE_CATEGORIES_TABLE2 = "CREATE TABLE " + TABLE_LABELS2 + "("
                + KEY_11+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_1+ " TEXT," + KEY_2+ " TEXT," + KEY_3+ " TEXT)";
        db.execSQL(CREATE_CATEGORIES_TABLE);
        db.execSQL(CREATE_CATEGORIES_TABLE1);
        db.execSQL(CREATE_CATEGORIES_TABLE2);
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS1);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS2);
        // Create tables again
        onCreate(db);
    }

    /**
     * Inserting new lable into lables table
     * */



public void insertlabel(String text,String id9,String id, String label) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(KEY_99,text);
    cv.put(ROUTE,id9);
    cv.put(KEY_ID,id);
    cv.put(KEY_NAME,label);
    db.insert(TABLE_LABELS,null,cv);
    db.close();
}


    public void insertLabel(String message1, String message2,String message3){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_1, message1);
        values.put(KEY_2, message2);
        values.put(KEY_3,message3);
        // Inserting Row
        db.insert(TABLE_LABELS2, null, values);
        //db.close(); // Closing database connection
    }

    public void insertLabel1(String label){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME1, label);
        db.insert(TABLE_LABELS1, null, values);
        //db.close(); // Closing database connection
    }

    public void insertLabel2(String label){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, label);
        db.insert(TABLE_LABELS, null, values);
        db.close(); // Closing database connection
    }


    public Cursor getAllLabelsForSpinner1AsCursor() {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(TABLE_LABELS,columns,null,null,null,null,null);
    }


    public Cursor getAllLabelsForSpinner2AsCursor(String keyFromSinner1) {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(
                TABLE_LABELS,columns,
                DatabaseHandler.ROUTE + " LIKE ?",
                new String[]{keyFromSinner1+"%"},
                null,null,null
        );
    }

    public Cursor getAllLabelsForSpinner3AsCursor(String keyFromSpinner2) {
        String[] columns = new String[]{"rowid AS _id, *"}; // Need _id column for SimpleCursorAdapter
        return this.getWritableDatabase().query(
                TABLE_LABELS,columns,
                DatabaseHandler.ROUTE + " LIKE ?",
                new String[]{keyFromSpinner2 + "%"},
                null,null,null);
    }
}

3 个答案:

答案 0 :(得分:1)

从中的行尾删除分号

while (!"Done".equals(answer));

分号本身就是一个空语句,它成为while循环的主体。结果,花括号中的块不再属于您的while循环。

在IDE中使用自动格式化/缩进会显示问题。它会将分号放在缩进的下一行。

答案 1 :(得分:1)

我测试了您的代码,并与上述问题一起在行尾添加了冒号:while (!"Done".equals(answer));,您使用“完成”一词停止程序的代码无法正常工作。

您每次都要两次检查“完成”字词,尽管只需要检查一次即可。

    System.out.println("Enter the value: (Type 'done' to stop)");
    Scanner input = new Scanner(System.in);
    String answer = input.next();

    while (!answer.equalsIgnoreCase("done")) {
        char ch1 = answer.charAt(0);
        if ((ch1 >= '0' && ch1 <= '9'))
            System.out.println("Number");
        else if ((ch1 >= 'a' && ch1 <= 'z'))
            System.out.println("Small Character");
        else if ((ch1 >= 'A' && ch1 <= 'Z'))
            System.out.println("Capital Character");
        else
            System.out.println("Symbol");

        System.out.println("Enter the value: (Type 'done' to stop)");
        answer = input.next();
    }

答案 2 :(得分:0)

除了while循环中的空语句外,您还一起使用nextLine()next()。调用next()并不会占用行尾的换行符,这会导致后面的nextLine()仅读取换行符。除非您以相同的方式读取每一行,否则您将无法停止循环。