How to implement a java tester class - java

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 ?

Related

Scanner input string appearing as null java

My code is here:
package test1;
import java.util.*;
public class Test1 {
public static String input;
public Test1(){
Scanner answer = new Scanner(System.in);
String test = answer.next();
}
public static void initializeConstructor(){
Test1 input = new Test1();
}
public static void begin () {
System.out.println("type:");
initializeConstructor();
System.out.println(input);
}
public static void main(String[] args) {
begin();
}
}
I am really new to learning java, my idea is that I can call the constructor to to start the scanner and it will spit back at me what I just typed. I am doing this so I can understand more about constructors in java. However when I run the following program, it just gives me "null". Like I said, i am preety new, so it may be a dumb question but any response would be very appreciated. Thank you in advance.
Because input is null (never assigned), change
String test = answer.next();
to
input = answer.next();
and your code will work. But input is static (it shouldn't be in real code, not if you're setting it in a constructor).

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");
}

Connecting 2 classes

I have a student object class and I have to create an arraylist of these students.
Im trying to create the program that inputs information into the student object then store it into an arraylist.
The first command to this array is to add a student object to the array the next command would be to remove a student from the array.
How would I get a user to input a command then go to another method that leads to storing information if everytime i try to complie i get static errors.
import java.util.Scanner;
import java.util.ArrayList;
public class CollegeTester
{
Scanner input = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
CollegeTester collegeTester = new CollegeTester();;
public static void main(String[] args)
{
new CollegeTester().getCommand();
}
public void getCommand()
{
System.out.println("Enter a command: ");
String command = input.nextLine();
if(command.equals("add"))
collegeTester.addCommand();
}
public void addCommand()
{
System.out.println("Enter a Name: ");
String name = input.nextLine();
}
}
There is another loophole. Your main method is not static. Your program will not run without it. Change it to
public static void main(String[] args)
{
}
Also then you cannot access non-static methods directly from static method.
Either you should instantiate class and then call methods or make methods static.
public static void main(String[] args){
CollegeTester collegeTester = new CollegeTester();
collegeTester.addCommand();
}
If you want to call specific method on specific options, then you should use switch case.
You are trying to access to not static method from static method. At first make an instance of CollegeTester class, than call it. For example:
import java.util.ArrayList;
import java.util.Scanner;
public class CollegeTester {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> array = new ArrayList<Student>();
public static void main(String[] args) {
new CollegeTester().getCommand();
}
public void getCommand() {
System.out.println("Enter a command: ");
String command = input.nextLine();
if (command.equals("add"))
this.addCommand();
}
public void addCommand() {
System.out.println("Enter a Name: ");
String name = input.nextLine();
}
}
public class Student {
// Some properties and fty.
}

calling main method from another method in the same class

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

Categories