Solved

java call

Posted on 2009-04-03
23
257 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 89
What is the Best Editor for PHP Development ? 5 74
couple of eclipse 5 36
Unhandled exception type Exception 18 31
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
This video teaches viewers about errors in exception handling.
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 …

770 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