开发者

pass the code of java to work for java/android

开发者 https://www.devze.com 2023-04-08 15:27 出处:网络
I have 3 .java files: main.java, separetdat.java and token.java main.java import java.util.*; public class Main {

I have 3 .java files: main.java, separetdat.java and token.java

main.java

import java.util.*;

public class Main {
public static void main(String[] args) {
    Tokenizer ob1=new Tokenizer();
    LinkedList listaDeCoord=new LinkedList();
    SepararDatos oSepararDatos=new SepararDatos();
    ob1.leerPath();
    ob1.getDataFromFile();

    listaDeCoord=ob1.listaTokens;
    listaDeCoord=oSepararDatos.getLinkedList(listaDeCoord);
    double[] vectorDeDatos; //= new double[listaDeCoord.size()];
    double[] vectorIndex;


    vectorDeDatos=oSepararDatos.getArrayData(listaDeCoord);


    vectorIndex=oSepararDatos.getArrayIndex(vectorDeDatos);      

}
}

separetdat.java:

import java.util.*;

public class SepararDatos {

public void SepararDatos()
{

}

public double[] getArrayIndex(double[] vector)
{
    double[] vectorIndex=new double[vector.length/3];
    int index=0,i=0;

    while (index<vector.length)
    {
        vectorIndex[i]=vector[index];
        index=index+3;
        i++;
    }

    return vectorIndex;
}

     public LinkedList getLinkedList(LinkedList lista)
{
    String linea="";
    StringTokenizer st;
    String palabra="";
    LinkedList palabras=new LinkedList();
    //lista=new LinkedList();
    int j=0;
    char c;
    int indice=0;

    for (int i=0;i<lista.size();i++)
    {
        linea=lista.get(i).toString();
        st=new StringTokenizer(linea);
        while (st.hasMoreTokens())
        {
            palabras.add((st.nextToken()));
        }
    }

return palabras;
}



public double[] getArrayData(LinkedList lista)
{
    double[] vectorPalabras=new double[lista.size()];


    for (int i=0;i<vectorPalabras.length;i++)
    {

        vectorPalabras[i]=Double.parseDouble(lista.get(i).toString());

    }

return vectorPalabras;
}
}

token.java

import java.io.*;
import java.util.*;

public class Tokenizer {

String strFile;
BufferedReader br;
String strLine;
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
LinkedList lista=new LinkedList();
LinkedList listaTokens=new LinkedList();


public void Tokenizer()
{

}

public void leerPath ()
{
    strFile = "path of the txt file.txt";
    try
    {
        br = new BufferedReader( new FileReader(strFile));
    }
    catch(Exception e)
{
        System.out.println("Exception while reading csv file: " + e);
}

}

public double[] getDataFromFile()
{
    double[] vector=new double[lista.size()];

    try
    {
        while( (strLine = br.readLine()) != null)
        {
            lineNumber++;
    //break comma separated line using ","
    //st = new StringTokenizer(strLine, ",");
    st = new StringTokenizer(strLine);
            while(st.hasMoreTokens())
    {
                //display csv values

                System.out.println("Line # " + lineNumber + ", Token # " + tokenNumber + ", Token : "+ st.nextToken());

                tokenNumber++;
    }
                //reset token number
                tokenNumber = 0;
                lista.add(strLine);
        }
    }
    catch(Exception e)
{
        System.out.println("Exception while reading csv file: " + e);
}
    listaTokens=lista;


    for (int i=0;i<vector.length;i++)
    {

        vector[i]=Double.parseDouble(lista.get(i).toString());

    }

    return vector;
}
}

When I create the java project, it works perfectly, but when I create an Android project, it does not work. What do I need to change the original java code so that it works in Android?


im sorry for my question to be how to put it mmmm dumb basically what i like to now is how to call 开发者_运维百科the methods from the other 2 clases in Activity, activity has to call form the other 2 classes, simple example will be let say activity.java has to call from calculate.java the method that prints out the result

activity.java calls result from sum calls result from multiply prints the results

name.java let say it does a sum in one function return sum result.

2 function does multiply 2 numbers returm multiply results.


I've never liked answered on here that are simply "Read the documentation" followed by links, but I think that's the best answer in this case.

I strongly recomment reading this doc and this doc on application fundamentals to get an idea of how the android framework works, as well as following the tutorials on the android dev site. I'm sure there is nothing wrong with you code, but the android framework doesn't work like java does.


The code you posted is plain old Java SE code that has "System.out.println()" statements to print to the console. How were you expecting to run this on Android?

Android apps are written in Java, but they have to be built as an Android application. You can't just take random Java code and expect it to magically run on an Android device.

StackOverflow is also for providing information to help you program... it's not for people to "give you the code", and it doesn't seem like you've done enough research to even know how to use advice people could give you...

There are tons of resources to start learning Android, just search for them on the web. But at least to start you off, here's the Android Dev Guide.


The structure of an Android project is entirely different from a regular Java project. Android has no main, instead it has a main Activity. There are also many other differences; basically too many to cover in a single answer.

You should check out Google's Android Developer's Guide. Then check out the Notepad tutorial. This will give you an idea as to how you can convert your project over to Android. The Notepad tutorial especially, will give you an idea as to how an Android project is structured, and how the various components communicate with each other.

0

精彩评论

暂无评论...
验证码 换一张
取 消