Solved

java call

Posted on 2009-04-03
23
256 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
Comment Utility
whats the problem you are havng now?
0
 

Author Comment

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

Expert Comment

by:ksivananth
Comment Utility
can you post the code?
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
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
Comment Utility
thks, i will try this
0
 

Author Comment

by:skywaker
Comment Utility
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
Comment Utility
what is the error?
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>ProcessKey process_Key=processKey;

that should be

ProcessKey process_Key=new processKey() ;
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>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
Comment Utility
>>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
Comment Utility

Error when i build
Symbol  : class ProcessKey
location: class getlastkey.Main
         ProcessKey process_Key= new processKey() ;
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>ProcessKey process_Key= new processKey() ;

try

processKey process_Key= new processKey() ;
0
 

Author Comment

by:skywaker
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your second file name is 'NewClass.java' or 'key.java' ?
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
/*
 * 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
import  <pkg>.key

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 83
groovy example issue 10 64
JAVA part two 5 39
Java array passed to SQL where clause 23 38
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now