Solved

java call

Posted on 2009-04-03
23
260 Views
Last Modified: 2013-12-14
I am new to java , i need to do the folowing . 1] Create a java app this must allow user to input a key  eg 'A9BC1De'. 2] create a java class to do some calulations on the the key. The app and class must be seperate. A] I need to call the java class from the java app,  b]pass the key variable to the java class c] return a new key value from the java class . show the new key value in java app . eg 'new key is '; . I am working with netbeans
0
Comment
Question by:skywaker
  • 9
  • 8
  • 6
23 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059183
whats the problem you are havng now?
0
 

Author Comment

by:skywaker
ID: 24059212
i can create the app , but i dont now how to call the class
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059219
can you post the code?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 24059254
usually you will call a class by creating instance, for e.g., if you new class is

public class NewClass{

  public String calculate( String key ){
    String newVal = null ;
//do the calc here
    return newVal ;
  }
}

then in you app,

NewClass newClass = new NewClass() ;
::::::::::
System.out.println( "New Value: " + newClass.calculate( key ) ) ;
0
 

Author Comment

by:skywaker
ID: 24059299
thks, i will try this
0
 

Author Comment

by:skywaker
ID: 24059391
Hi When i try entering the new instance in the class it show as error , code snippet here

Java Application
*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package getlastkey;
 
/**
 *
  */
import java.io.*;
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         ProcessKey process_Key=processKey;
      //  prompt the user to enter their name 
      System.out.print("Enter The Last Key: "); 
 
      //  open up standard input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 
      String lastKey = null; 
 
      //  read the username from the command-line; need to use try/catch with the 
      //  readLine() method 
      try { 
         lastKey = br.readLine(); 
      } catch (IOException ioe) { 
         System.out.println("IO error trying to read your Key!"); 
         System.exit(1); 
      } 
 
      System.out.println("The Next Key Is , " + lastKey); 
    }
 
}
 
Java Class
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author patMoodley
 */
public class processKey {
    int counter_A;
    int counter_B;
    String LengthOfKey;
    String NextKey;
    String InputKey;
  public String calculate(String OldKey){
      
      NextKey="KRB0132";
      return NextKey;
   }

Open in new window

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059411
what is the error?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059424
>>ProcessKey process_Key=processKey;

that should be

ProcessKey process_Key=new processKey() ;
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059434
>>System.out.println("The Next Key Is , " + lastKey);

should be

System.out.println("The Next Key Is , " + process_Key.caculate( lastKey ) );
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24059449
>>public String calculate(String OldKey){
     
      NextKey="KRB0132";
      return NextKey;
   }
>>

here you are just returning the old value, not doing any calculations!
0
 

Author Comment

by:skywaker
ID: 24059487

Error when i build
Symbol  : class ProcessKey
location: class getlastkey.Main
         ProcessKey process_Key= new processKey() ;
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24060454
>>ProcessKey process_Key= new processKey() ;

try

processKey process_Key= new processKey() ;
0
 

Author Comment

by:skywaker
ID: 24061479
Ps: The newclass needs to be compiled , which i will distribute for other developers will use in there java apps
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package testapp;
import java.io.*;
 
/**
 *
 * @author patMoodley
 */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         //  prompt the user to enter their name 
       
 
      System.out.print("Enter The Last Key: "); 
 
      //  open up standard input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 
      String lastKey = null; 
 
      //  read the username from the command-line; need to use try/catch with the 
      //  readLine() method 
      try { 
         lastKey = br.readLine(); 
      } catch (IOException ioe) { 
         System.out.println("IO error trying to read your Key!"); 
         System.exit(1); 
      } 
      NewClass newClass = new NewClass() ;
 
      System.out.println("The Next Key Is , " + lastKey); 
    }
 
    }
 
 
public class NewClass {
public String calculate( String Oldkey ){
    String newVal = null ;
//do the calc here
    return newVal ;
  }
 
}

Open in new window

0
 
LVL 12

Expert Comment

by:basav_com
ID: 24063596
public class NewClass {   ---> Change this to class NewClass

OR store this in a separate file.

A javafile can have only 'one' public class.
0
 

Author Comment

by:skywaker
ID: 24063639
Hi : I have 2 files 1 is the app  2  is the class file . I want to compile the class file and distribute it . Users would create thier own App that would call this file. I need to know how to do this
0
 
LVL 12

Accepted Solution

by:
basav_com earned 500 total points
ID: 24063815
You need to store this in ( 2 files)
public class Main   --->  Main.java
public class NewClass  --->  NewClass.java


As Main class has main method, declare this in the manifest file.
0
 

Author Comment

by:skywaker
ID: 24064039
Hi does not allow me to upload the files. BuT i already have 2 files.
1st File. Main.java this is the app
2nd file. key.class . i have compiled this file. now i want to call it from main.java

File 1] main.java
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package getnextkey;
import java.io.*;
 
/**
 *
  */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         //  prompt the user to enter their name 
       
 
      System.out.print("Enter The Last Key: "); 
 
      //  open up standard input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 
      String lastKey = null; 
 
      //  read the username from the command-line; need to use try/catch with the 
      //  readLine() method 
      try { 
         lastKey = br.readLine(); 
      } catch (IOException ioe) { 
         System.out.println("IO error trying to read your Key!"); 
         System.exit(1); 
      } 
  
 
      System.out.println("The Next Key Is , " + lastKey); 
    }
 
    }
------------------------------------------------------------
File 2 This file would be compiled
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author patMoodley
 */
public class NewClass  {
   
public String calculate( String Oldkey ){
    String newVal = null ;
//do the calc here
    return newVal ;
  }
 
}

Open in new window

0
 
LVL 12

Expert Comment

by:basav_com
ID: 24065848
Your second file name is 'NewClass.java' or 'key.java' ?
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24065862
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package getnextkey;
import java.io.*;
 
/**
 *
  */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         //  prompt the user to enter their name
       
 
      System.out.print("Enter The Last Key: ");
 
      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
      String lastKey = null;
 
      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         lastKey = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your Key!");
         System.exit(1);
      }
 
 
      System.out.println("The Next Key Is , " + lastKey);
     
      NewClass newclass = new NewClass();
      String newKey = newclass.calculate(lastKey);
      System.out.println(newKey);
    }
 
    }
//------------------------------------------------------------
//File 2 This file would be compiled
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author patMoodley
 */
class NewClass  {
   
            public String calculate( String Oldkey ){
    String newVal = null ;
//do the calc here
    newVal = "Iam new Key";
    return newVal ;
  }
 
}
0
 

Author Comment

by:skywaker
ID: 24066009
Hi the second file is named key.class not key.java . The key.class is compiled .  Iam trying to create a file simalar to a dll that i want to distribute. This must be compiled. No one would have access to the source.
1] is this possible in java.
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24066016
Just 'import' the key class from Main.java and call the required method. You don't need to have the source code of key.class

If you are really curious to know wat is inside the key.class  you can use java decompiler.
http://java.decompiler.free.fr/
0
 

Author Comment

by:skywaker
ID: 24066063
this is where my problem lies , i do not know how to import the key class. I am developing the key.class for distribution.
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24066105
import  <pkg>.key

in your Main.java program
http://leepoint.net/notes-java/language/10basics/import.html
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question