java call

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
skywakerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
basav_comConnect With a Mentor Commented:
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
 
ksivananthCommented:
whats the problem you are havng now?
0
 
skywakerAuthor Commented:
i can create the app , but i dont now how to call the class
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ksivananthCommented:
can you post the code?
0
 
ksivananthCommented:
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
 
skywakerAuthor Commented:
thks, i will try this
0
 
skywakerAuthor Commented:
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
 
ksivananthCommented:
what is the error?
0
 
ksivananthCommented:
>>ProcessKey process_Key=processKey;

that should be

ProcessKey process_Key=new processKey() ;
0
 
ksivananthCommented:
>>System.out.println("The Next Key Is , " + lastKey);

should be

System.out.println("The Next Key Is , " + process_Key.caculate( lastKey ) );
0
 
ksivananthCommented:
>>public String calculate(String OldKey){
     
      NextKey="KRB0132";
      return NextKey;
   }
>>

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

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

try

processKey process_Key= new processKey() ;
0
 
skywakerAuthor Commented:
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
 
basav_comCommented:
public class NewClass {   ---> Change this to class NewClass

OR store this in a separate file.

A javafile can have only 'one' public class.
0
 
skywakerAuthor Commented:
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
 
skywakerAuthor Commented:
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
 
basav_comCommented:
Your second file name is 'NewClass.java' or 'key.java' ?
0
 
basav_comCommented:
/*
 * 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
 
skywakerAuthor Commented:
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
 
basav_comCommented:
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
 
skywakerAuthor Commented:
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
 
basav_comCommented:
import  <pkg>.key

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

From novice to tech pro — start learning today.