skywaker
asked on
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
whats the problem you are havng now?
ASKER
i can create the app , but i dont now how to call the class
can you post the code?
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 ) ) ;
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 ) ) ;
ASKER
thks, i will try this
ASKER
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;
}
what is the error?
>>ProcessKey process_Key=processKey;
that should be
ProcessKey process_Key=new processKey() ;
that should be
ProcessKey process_Key=new processKey() ;
>>System.out.println("The Next Key Is , " + lastKey);
should be
System.out.println("The Next Key Is , " + process_Key.caculate( lastKey ) );
should be
System.out.println("The Next Key Is , " + process_Key.caculate( lastKey ) );
>>public String calculate(String OldKey){
NextKey="KRB0132";
return NextKey;
}
>>
here you are just returning the old value, not doing any calculations!
NextKey="KRB0132";
return NextKey;
}
>>
here you are just returning the old value, not doing any calculations!
ASKER
Error when i build
Symbol : class ProcessKey
location: class getlastkey.Main
ProcessKey process_Key= new processKey() ;
>>ProcessKey process_Key= new processKey() ;
try
processKey process_Key= new processKey() ;
try
processKey process_Key= new processKey() ;
ASKER
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 ;
}
}
public class NewClass { ---> Change this to class NewClass
OR store this in a separate file.
A javafile can have only 'one' public class.
OR store this in a separate file.
A javafile can have only 'one' public class.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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 ;
}
}
Your second file name is 'NewClass.java' or 'key.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.i n));
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 ;
}
}
* 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.i
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 ;
}
}
ASKER
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.
1] is this possible in java.
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/
If you are really curious to know wat is inside the key.class you can use java decompiler.
http://java.decompiler.free.fr/
ASKER
this is where my problem lies , i do not know how to import the key class. I am developing the key.class for distribution.
import <pkg>.key
in your Main.java program
http://leepoint.net/notes-java/language/10basics/import.html
in your Main.java program
http://leepoint.net/notes-java/language/10basics/import.html