[SOLVED] How to check the code for errors so the app will work properly ?

Status
Not open for further replies.
Nov 22, 2022
12
1
15
I wrote the program code, but when the application starts, this part is not displayed (code below). Finding and correcting errors takes a lot of time, which can be applied more efficiently. How do you fix bugs in your code? Are there special tools?
Code:
package com.TSP;

import java.io.IOException;
import java.util.ArrayList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.geometry.Point2D;
import javafx.stage.Stage;

public class Controller {

    @FXML
    public static AnchorPane canvasPane;

    private static ArrayList<Point2D> pointsArray;

    private int newCitiesCount; //в дальнейшем буду передавать его как bound для ProblemSolver

    public static Point2D canvasPanePointCoords;

    public static Point2D prevPointClicked;

    public static int getCity(Point2D point) {
            int i;
            for (i = 0; i < pointsArray.size(); ++i) {
                if (pointsArray.get(i).distance(point) < 24)
                    break;
            }
            return i + 1;
    }

    private int repPointClicksCounter;

    private static Stage stage;

    private static void showDistanceInputWindow() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("dialog.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 400, 250);
        stage.setTitle("Distance input");
        stage.setScene(scene);
        stage.show();
    }

    public static void closeDistanceInputWindow() {
        stage.close();
    }

    @FXML
    void onMouseClicked(MouseEvent event) {
        double pointX = event.getSceneX();
        double pointY = event.getSceneY();
        canvasPanePointCoords = canvasPane.sceneToLocal(pointX, pointY);
        int counter = 0; int counter2 = 0;
        int flag = 0;
        if (pointsArray.size() == 0)
            flag = 1;
        else {
            for (int i = 0; i < pointsArray.size(); ++i) {
                if (pointsArray.get(i).distance(canvasPanePointCoords) > 24)
                    ++counter;
            }
            if (counter == pointsArray.size())
                flag = 1;
        }
        if (flag == 1 && newCitiesCount < CitiesCollection.size) { //если в месте клика нет точки
            pointsArray.add(canvasPanePointCoords);
            Circle point = new Circle(canvasPanePointCoords.getX(), canvasPanePointCoords.getY(), 8, Color.CADETBLUE);
            Label label = new Label(Integer.toString(newCitiesCount));
            label.setLayoutX(canvasPanePointCoords.getX()+5);
            label.setLayoutY(canvasPanePointCoords.getY()+5);

            canvasPane.getChildren().add(point);
            canvasPane.getChildren().add(label);
            System.out.println("Added new city: (" + canvasPanePointCoords.getX() + " ; " + canvasPanePointCoords.getY() + ")");
            newCitiesCount++;
        } else if (flag == 0) { //если в месте клика уже есть точка
            if (pointsArray.size() > 1) { //если пред. клик был не в эту же точку и имеется больше 1 точки
                repPointClicksCounter++;
                if (repPointClicksCounter == 2) {
                    for (int i = 0; i < pointsArray.size(); ++i) {
                        if (pointsArray.get(i).distance(canvasPanePointCoords) < 24 || pointsArray.get(i).distance(prevPointClicked) < 24)
                            ++counter2;
                    }
                    repPointClicksCounter = 0;
                }
                if (counter2 == 2 && prevPointClicked.distance(canvasPanePointCoords) > 24) { //если пред. клик и текущий клик - города
                    try {
                        showDistanceInputWindow();
                    }
                    catch (IOException e) {
                        System.out.println("Wrong input!");
                    }
                }
            }
        }
        prevPointClicked = canvasPanePointCoords;
    }

    @FXML
    void onStartButtonClicked(MouseEvent event) {
        CitiesCollection.output();
        ProblemSolver.findShortestRoute(CitiesCollection.matrix, newCitiesCount).printRoute();
    }

    @FXML
    void initialize() {
        CitiesCollection.createCitiesCollection();
        pointsArray = new ArrayList<>();
        canvasPane = new AnchorPane();
        prevPointClicked = new Point2D(0,0);
        canvasPanePointCoords = new Point2D(0,0);
        newCitiesCount = 1;
        repPointClicksCounter = 0;
        stage = new Stage();
    }
}
 
D

Deleted member 14196

Guest
Use the debugger. Use NetBeans as your development environment.

with NetBeans you can compile the code and it will show you the errors and you will be able to correct them. then you can use the debugger to step through your code and make sure your logic doesn’t need fixing

I recommend open Java version 15.
 
Last edited by a moderator:
Nov 22, 2022
12
1
15
Use the debugger. Use NetBeans as your development environment.

with NetBeans you can compile the code and it will show you the errors and you will be able to correct them. then you can use the debugger to step through your code and make sure your logic doesn’t need fixing

I recommend open Java version 15.
Okay, I'll try.
I also found a service on the Internet AppRefactoring for code analysis, have you tried it?
 
D

Deleted member 14196

Guest
No. The best thing for you to do is learn the language that you want to program the way that I did it is to use the tutorials that come with Java. And then using NetBeans, you can work through all the examples and actually learn how everything works.
 
  • Like
Reactions: Ralston18
D

Deleted member 14196

Guest
OK here’s what I would do if I was you.
Step one is to create a virtual machine because this is going to be where you do your Step twolearning and development.

Step two is install your operating system into the virtual machine of course and once that’s done, you will want to set up an Apache Web server and also install NetBeans and Open JDK 15

Step three snapshot your virtual machine at this point so you can always roll back to this snapshot and also back this whole virtual machine up to another drive so you don’t ever lose it

Step four is for learning and developing your job applications or web service by executing all of the examples as you read through the tutorial found in Java.

Step five is to have fun! This is the super fun part, dude, cherish it. Let me tell you something don’t ever dedicate your life to entertainment. spend your life learning, and you will be so happy and content, and once you learn Java, you basically can do any language. You just have to learn the syntax and such

this can be done either in windows or linux. It’s your choice.

in the very earliest days, the first releases of C# was a java clone. Just by changing the case of a method calls, you could make the code run in either. It’s not the case anymore but once you learn Java, you could easily transition to C#, which is a very well paid for service if you can provide it— meaning if you’re good, you can get a good job
 
Last edited by a moderator:
  • Like
Reactions: Ralston18
Nov 22, 2022
12
1
15
OK here’s what I would do if I was you.
Step one is to create a virtual machine because this is going to be where you do your Step twolearning and development.

Step two is install your operating system into the virtual machine of course and once that’s done, you will want to set up an Apache Web server and also install NetBeans and Open JDK 15

Step three snapshot your virtual machine at this point so you can always roll back to this snapshot and also back this whole virtual machine up to another drive so you don’t ever lose it

Step four is for learning and developing your job applications or web service by executing all of the examples as you read through the tutorial found in Java.

Step five is to have fun! This is the super fun part, dude, cherish it. Let me tell you something don’t ever dedicate your life to entertainment. spend your life learning, and you will be so happy and content, and once you learn Java, you basically can do any language. You just have to learn the syntax and such

this can be done either in windows or linux. It’s your choice.

in the very earliest days, the first releases of C# was a java clone. Just by changing the case of a method calls, you could make the code run in either. It’s not the case anymore but once you learn Java, you could easily transition to C#, which is a very well paid for service if you can provide it— meaning if you’re good, you can get a good job
Wow, thanks a lot for this detail!
 
Status
Not open for further replies.