Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Java Basic Rules


// file name MyFirstClass.java

import java.util.*;
import java.lang.Math;
import java.io.*;
import javax.swing.*;

public class MyFirstClass{ // start of program
  public static void main(String[] args) { // basic  function main

// variables and calculation
int a=2;
int b=3;
int c=Integer.parseInt(JOptionPane.showInputDialog(" Put number: ", "1"));
System.out.println("Number is: "+c);
System.out.println(a+" * "+b+" = "+(a*b));
System.out.println("a^3 "+Math.pow(a,b));

//array
int[] array_my=new int[10];
array_my[0]=3;
array_my[1]=5;
System.out.println("Number of elements "+array_my.length+" 1 + 2 element of array "+(array_my[0]+array_my[1]));

//strings
String txt="Quick red fox";
String txt2=JOptionPane.showInputDialog("Write text: ", "word");
System.out.println("Text is: "+txt2);
String[] count_of_word=txt.split(" ");
System.out.println("Length: "+txt.length()+" Count of words "+count_of_word.length);
System.out.println(txt +" -> "+txt.replace("red","brown"));
System.out.println("First 5 chars of string is: "+ txt.substring(0,5));

//for a if
for(int i=0; i<10;i++){
	if(i==3)System.out.println("i equal "+i);
	}

// file
try {
  File soubor=new File("some_file.txt");
  if(!soubor.exists()){
	System.out.println("File dont exist");
  }
  else { // utf-8 encoding
    BufferedReader in1 = new BufferedReader(new InputStreamReader(new FileInputStream(soubor),"UTF-8"));
    BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("zapis.txt"),"UTF-8"));
    String str;
      while ((str = in1.readLine())!=null){
        System.out.println(str);
        out1.write(str+"

"); } in1.close(); out1.close(); } } catch(IOException e){System.out.println("Error " + e);} // dir File pathName = new File("some_dir"); String[] fileNames = pathName.list(); if(pathName.exists()) System.out.println("Name of first file in "some_dir": "+fileNames[0]); else System.out.println("dir not exist"); //function int nResult = calculateMyFc(3,5); System.out.println("Result of function: "+nResult); } // end of function main // new function , you can add to end MyFc = my function public static int calculateMyFc(int a, int b){ return (a+b); } } // end of class of program

396LW NO topic_id




AD

Další témata ....(Topics)


223

Eclipse my templates store Android | eclipse-my-templates-store-android


Where file(s) does eclipse store Java code style settings to?
If you need copy any old templates to the new workspace.

workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.ui.prefs


You can edit the Eclipse Templates from menu Window - Preferences - Java - Editor - Templates.
24

Sqlite create database and table with BAT file for Android | sqlite-create-database-and-table-with-bat-file


Sqlite3 create database and table with load.bat file and fill data to table example.


  1. Create folder for your project: my_sqlite_project

  2. Open folder and create file load.bat and paste to load.bat this text and save to project folder:


    sqlite3 my_database.s3db < load_text.sql
    pause



  3. Create load_text.sql file and paste this text and save to project folder:

    CREATE TABLE [android_metadata] (
    [locale] TEXT
    );




    CREATE TABLE [my_table] (
    [_id] int NULL,
    [word] VARCHAR(255) NULL,
    [description] VARCHAR(255) NULL




    .separator ";"
    .import text_file.txt my_table


  4. Create text_file.txt and paste this text and save it as UTF-8:

    1;word1;my first word
    2;word2; my second word



  5. Download sqlite3.exe and put to project folder.

  6. Run BAT file load.bat and read text instruction from console

  7. If database created you can open and edit this with sqlite database explorer

  8. Copy database to Asses Android project folder

  9. If you want using this database in Android application on device, you have to copy this database to folder on device /data/data/com.MyPackage/databases/









257

Delete Bitmap Android Example | delete-bitmap-android-example


If some memory leak - problem try release of memory used of a big Bitmap what already not to need;
Bitmpap bmp; // not null
bmp.recycle();
bmp = null;

final boolean bmpIsRecycled = bmp.isRecycled()
// Returns true if this bitmap has been recycled.


//developer.android.com/reference/android/graphics/Bitmap.html
Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead”, meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.
221

android - OnClickListener must override a superclass method | android-onclicklistener-must-override-a-superclass-method


The method onClick(DialogInterface, int) of type new DialogInterface.OnClickListener(){} must override a superclass method.

Try this:
Right click on project
Select Properties
In open dialogue select Java compiler
Set Enable project specific settings
Set Compiler compliance level to 1.6

android/project-properties-eclipse.png

android/java-compiler-settings-eclipse.png