• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Moving code from running from the command line to running within a larger java program

 
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have installed NetBeans and now understand how to run stand alone programs by passing any necessary arguments

Is there a standard way to incorporate such programs into a larger java program ?
(i.e. such that the code is run from within the larger program)

Bob M
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's hard to answer without seeing what exactly you have, but remember...

your main method is just like ANY other method. You can explicitly call it if you want from another class.

If your class is written correctly, you would create an object of your current class, and you could simply call its methods.

Does that help?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred

My code starts as follows:-

public class Tokenize {

public static void main(String[] args) {
// read the input file
if(args.length != 1) {
System.err.println("pass the name of the file to be read as an argument");
System.exit(-1);
}
String fileName = args[0];
// use a TreeSet<String> which will automatically sort the words
// in alphabetical order
Set<String> words = new TreeSet<>();

Bob M
 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to think of creating objects and see the main method as a way of testing or interacting with them. Here is an example creating an instance of a class and calling it's methods like Fred said:



See how the main method driver gives an example of how one might use MyClass as part of a larger program?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jacob

I think I understand..........................
I currently have the following code:-


When I run the large java program I get the following output:-

headlines2: [] ???

Bob M
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Matthews wrote:
When I run the large java program I get the following output:-

headlines2: [] ???



Well, were you expecting your tokenizer doIt() method to return an empty set? If not, you will need to debug it to figure out why.

Henry
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry

No I am not expecting an empty string

As I have stated before, the tokenizing method works OK as a standalone in Netbeans

It is when I try the above code that get a problem

Bob M
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic