This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Rancher
Posts: 640
  • 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: 13025
66
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: 640
  • 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: 640
  • 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: 23912
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: 640
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic