这是我的代码我想读取文件milkinfo并在SQLite表中插入带有不同变量的数据。但是这行存在错误
public void main(String[] args) throws IOException {
错误是
Multiple markers at this line
- void is an invalid type for the variable main
- Syntax error on token "(", ; expected
- Syntax error, insert ";" to complete
LocalVariableDeclarationStatement
感谢您的支持:)
package com.super;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Userlevel1 extends ActionBarActivity {
// Тук се прави Таблицата!!!
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "lactoscan";
private static final int DATABASE_VERSION = 2;
// Database creation sql statement
private static final String DATABASE_CREATE = "CREATE TABLE milklog ( date integer," +
" time integer primary key, " +
"deliver integer, " +
"litres integer," +
"lsn integer," +
"calibr integer," +
"temp integer," +
"fat integer," +
"snf integer," +
"dencity integer," +
"lac integer," +
"soli integer," +
"prot integer);";
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
// Това, трябва да се махне във финалната версия, за да се запазят данните!!!
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){
Log.w(MyDatabaseHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS milklog");
onCreate(database);
}
private MyDatabaseHelper dbHelper;
private SQLiteDatabase database;
public final static String EMP_TABLE="MyEmployees"; // name of table
public final static String EMP_ID="_id"; // id value for employee
public final static String EMP_NAME="name"; // name of employee
/**
*
* @param context
*/
public void MyDB(Context context){
dbHelper = new MyDatabaseHelper(context);
database = dbHelper.getWritableDatabase();
}
public long createRecords(String id, String name){
public void main(String[] args) throws IOException {
File file = new File("/storage/emulated/0/Download/milkinfo.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while( (line = br.readLine())!= null ){
// \\s+ means any number of whitespaces between tokens
String [] tokens = line.split("\\s+");
String fdate = tokens[0];
String ftime = tokens[1];
String fdelivern = tokens[2];
String flitres = tokens[3];
String flsn = tokens[4];
String fcalibr = tokens[5];
String ftemp = tokens[6];
String ffat = tokens[7];
String fsnf = tokens[8];
String fdencity = tokens[9];
String flac = tokens[10];
String fsoli = tokens[11];
String fprot = tokens[12];
}
}
ContentValues values = new ContentValues();
values.put(EMP_ID, fdate);
values.put(EMP_NAME, name);
return database.insert(EMP_TABLE, null, values);
}
public Cursor selectRecords() {
String[] cols = new String[] {EMP_ID, EMP_NAME};
Cursor mCursor = database.query(true, EMP_TABLE,cols,null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor; // iterate to get each value.
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userlevel1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.userlevel1, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
除非你的问题中有拼写错误,否则main
应该被声明为静态方法。所以试试这个:
public static void main(String[] args) throws IOException {