calling main method from another method in the same class - java

How to call the main method?
void prompt()
{
System.out.println("Do you want to continue?");
Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con == "y")
{
//call the main method once again.
}
}
When I use
main();
It asks for the value of "args" yet I'm not sure what value I should put in it.

The main() method in a java program takes a String array argument.
public static void main(String[] args) {}
If you do not use the variable args inside of main() you could just pass null to it. Otherwise you would need to pass a String array to the method.
However, you should not be calling the main() method from inside your application. The main() method should be used as an entry point into your application, to launch a program, not be used to recursively execute the logic inside that application. If you have functionality needed again you should put it in a separate method.

Signature of main method is: public static void main(String[] args)
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. For example:
public static void main(String[] args) {
System.out.println("args = " + args);
}
public static void prompt() {
System.out.println("Do you want to continue?");
Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con == "y") {
String[] args = {<set string array>};
main(args);
}
}
For more details, look at this Oracle document: The main Method

Related

How to implement a java tester class

I implement an email system I have a main application class which is scanning some input from the keyboard and while input is not "q" ı have to able to add any command from console. The problem is how can i implement a tester class without changing the main application class. (I need to use already prepared commands within the tester class. They wont be entered explicitly, this means that when we test the code the prepared commands entered own its own). How can do this ? Or is it possible ?
I have 2 main method and I'm not allowed to use Junit.
this is the tester class:
public class CalcTester {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner =new Scanner(new File("C:\\Users\\burak kaya\\IdeaProjects\\Cacl\\src\\calctest.txt"));
String line ="";
for (int i = 0; scanner.hasNext() ; i++) {
line+=scanner.nextLine();
}
String[] arr =line.split(" ");
CalcApplication.main(arr);
}
application class is like that:
public class CalcApplication {
static Stack<Integer> number = new Stack<>();
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
String input =scanner.nextLine();
while(!input.equals("q")) {
if (input.equalsIgnoreCase("s")) {
} else if (input.equalsIgnoreCase("p")) {
} else {
}
}
}
}
I delete inside of the application because it was unnecessary.
My question is, the application works with scanner. On the other hand, the tester class has to use predefined commands to test this application class. How can i do that ?

working with String in java

In the given code,'s' is an object of class String.
When we pass 's' to a function,a copy of s will be created,which will be pointing to the same string.
but even then the changes made by the function are not getting reflected when done through method 1 but are getting reflected when we go by method 2.
Method 1:
class ST{
public static void main (String[] args) {
String s="Test";
change(s);
System.out.println(s);
}
static void change(String s)
{
s=s+"test";
}
}
Method 2:
class ST{
public static void main (String[] args) {
String s="Test";
s=change(s);
System.out.println(s);
}
static String change(String s)
{
return s+"test";
}
}
My question is, if every time a copy of the original reference is created, which is also pointing to the same string, then why does method one fail?
String objects are Immutables! You can't change them, so s=s+"test" creates a brand new String.
So the only way to manage it is your method number 2

I can compile this code and the build is successful, although nothing is printing out on the print line

public class TEST {
String name1 = "Kelly";
String name2 = "Christy";
String name3 = "Johnson";
public static void main(String[] args) {
}
public void displaySalutation(String name1) {
displayLetter();
}
public void displaySalutation(String name2, String name3) {
displayLetter();
}
public void displayLetter() {
System.out.println("Thank you for your recent order.");
}
}
The main method needs to make a call to the method containing the print line. This can be done by creating an instance of TEST which contains the methods to print. You then call the methods contained in the instance variable.
I've updated your code to show how to create an instance and call the method.
public class TEST {
String name1 = "Kelly";
String name2 = "Christy";
String name3 = "Johnson";
// Define the entry point
public static void main(String[] args) {
// Create an instance of test
TEST test = new TEST();
/* Call the displayLetter method within TEST
Prints "Thank you for your recent order." */
test.displayLetter();
/* Call the printName method with the "Kelly" String as an argument.
Prints "Kelly" */
test.printName(name1);
}
// Prints a line
public void displayLetter(){
System.out.println("Thank you for your recent order.");
}
// Example print variable
public void printName(String name){
System.out.println(name);
}
}
I've corrected your code so that you can call the displayLetter method but I'm unsure exactly what you would like to do with the other two methods.

Error: Main method not found in class MovieDatabase [duplicate]

This question already has answers here:
Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
(5 answers)
Closed 8 years ago.
Error: Main method not found in class MovieDatabase, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.Arrays;
public class MovieDatabase {
private int[] analysis;
//creating the contructor
public MovieDatabase(String file){
analysis = new int[2015];
this.load(file);
}
//uses the load(String file) method from downstairs to do all of the work
public void load(String file){
Scanner theScanner = null;
try{
//inputing the into the scanner
theScanner = new Scanner(new FileInputStream(file));
}
catch(Exception ex){
ex.printStackTrace();
}
// as long as the scanner has another line
while(theScanner.hasNextLine())
{
String Line = theScanner.nextLine();
//make an array called split and allocated different elements based on a seperation of ##
String split[] = Line.split("##");
int year = Integer.valueOf(split[1]);
analysis[year] ++;
}
}
//print out the array in the synchronous format
public void print(){
System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", "Year", "Occurances", "", "");
//go through the array
for (int i =0;i < analysis.length ;i++ ) {
if(analysis[i] >0){
for (int j =i;j < analysis.length ;i++ ){
System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", j, analysis[j], "", "");
}
}
}
}
}
How do I fix this error message?
Ive read other similar questions but just say to make the classes public. Mine are public.
main() method in Java is an standard method which is used by JVM to start execution of any Java program. main method is referred as entry point of Java application which is true in case of core java application
You have missed it. Add following main() method
public static void main(String[] args) {
MovieDatabase db = new MovieDatabase("file/path/goes/here");
db.print();
}
In the Java programming language, every application must contain a
main method whose signature is:
public static void main(String[] args)
As the error suggests, if its non FX project, just define something like:
public static void main(String args[]) {
...
}
Or else change you class definition so it extends Application, something like:
public class MovieDatabase extends Application
To invoke any application JVM need main() method, and which should have following access specifiers and modifiers,
public static void main(String args[])
public - It should be accessible to all
static - JVM not able to create instance of your class so method should be static
void - method not returning anything
For each java application main method is entry point, so your application should have atleast one main method to start execution of application.
It's because you have forgot to add the main method. The error clearly says:
please define the main method as: public static void main(String[]
args)
So add the main:
public static void main(String[] args) {
MovieDatabase m = new MovieDatabase("Your File Path");
m.print();
}

How to call a method with parameter from a diffrent class

Possible noob question but I cant get my method with parameters in one class to call in the other ?
FirstClass
public class Firstclass {
public static void main(String[] args) {
Test1 test = new Test1();
test.Passingvalue();
test.myMethod();
}
}
SecondClass
import java.util.Scanner;
public class Test1 {
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
String txtFile = Scan.next();
}
public void myMethod(String txtFile){
System.out.print("Scan this file" + txtFile);
}
}
You can provide the parameters as a comma separated list in the brackets after the method's name:
public static void main(String[] args) {
Test1 test = new Test1();
test.myMethod("my_file.txt");
}
Don't forget to add a parameter like this :
test.myMethod("txtFile");
declare your string txtfile as a public static variable outside the two methods (at the beginning of class test1) .
public class Firstclass {
public static void main(String[] args) {
Test1 test = new Test1();
test.Passingvalue();
test.myMethod();
}
}
import java.util.Scanner;
public class Test1 {
String txtFile;
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
txtFile = Scan.next();
}
public void myMethod(){
System.out.print("Scan this file" + txtFile);
}
}
I think you have a misconception here:
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
String txtFile = Scan.next(); //method scope only
}
Here the local variable txtFile only exists until the method Passingvalue (check naming conventions btw) is finished, i.e. it has method scope. Thus when calling myMethod(String txtFile) the parameter has the same name but is a different reference in a different scope.
So you'd either have to pass the file name to your method as the others already suggested or change the scope of txtFile, e.g. make it an instance variable:
public class Test1 {
private String txtFile; //the scope of this variable is the instance, i.e. it exists as long as the instance of Test1 exists.
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
txtFile = Scan.next();
}
public void myMethod(){
System.out.print("Scan this file" + txtFile);
}
}
Please note that this is just meant to illustrate the immediate problem. There are other issues, e.g. with the general design, which are not addressed. The purpose of your code seems to be learning anyways, so design is not that big an issue for now.
Just as a hint: I'd probably pass the name from outside the method or pass/read it in a constructor.
when you are calling a parameterize method you should have to pass a parameter to calling method other wise jvm will not understand to whom method you are calling becuase on the basis of parameters we can over load the methods .
so the final answer of your question is
public static void main(String[] args) {
Test1 test = new Test1();
test.myMethod("place your file name here");
}

Categories