Why am I able to call on String class methods with a String literal?

First of all, I am using the Drjava interactions pane to test all the codes that I am posting here(I do not have access to any other IDE in my current situation).
I have noticed that I am able to call methods that belong to the String class with string literals as well as string objects. That is,

String s1 = "a";  

s1 has access to all String methods such as getBytes(), length(), charAt(), etc.

While in the case of integers, an int variable doesn’t have access to the methods that are within the Integer class.

Why is this the case?

On a side note:

When I try to compare two string literals with the same value, as in

String s1 = "a";
String s2 = "b";

s1 == s2;

The boolean I get is false, which is not supposed to be the case according to the numerous post/articles I have read on string literals vs string objects.

Does this have something to do with how the Drjava functions specifically? Is that a part of why I am able to call on String methods with string literals?

Image on JButton

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Test extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                constructGUI();
            }
        });
    }

    private static void constructGUI() {
        JFrame frame = new JFrame("Testy");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.DARK_GRAY);
        centerPanel.setPreferredSize(new Dimension(100, 400));
        frame.add(centerPanel, BorderLayout.CENTER);

        Test eastPanel = new Test();
        frame.add(eastPanel, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    public Test() {

        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        Dimension d = new Dimension(50, 50);

        JButton button1 = new JButton("");
        button1.setPreferredSize(d);

        button1.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button1.setMaximumSize(new Dimension(Integer.MAX_VALUE, button1.getMinimumSize().height));
        add(button1);

        JButton button2 = new JButton("");
        button2.setPreferredSize(d);

        button2.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button2.setMaximumSize(new Dimension(Integer.MAX_VALUE, button2.getMinimumSize().height));
        add(button2);

        JButton button3 = new JButton("");
        button3.setPreferredSize(d);

        button3.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button3.setMaximumSize(new Dimension(Integer.MAX_VALUE, button3.getMinimumSize().height));
        add(button3);

        add(Box.createVerticalGlue());

    }

}

enter image description here

In my program I am trying to put the ellipse picture on top of all my buttons. As you can see in the image I posted, the ellipse.png is in the “Pictures” source folder.

However, the image does not appear on the JButtons for some reason.

I have read many posts but I can’t see a way to solve my problem.

Also, here is a link to the actual ellipse picture:

https://maxcdn.icons8.com/Share/icon/Editing//ellipse_stroked1600.png

Apache FOP 2.2 Include Fonts using Relative Path

I’m setting up a fop.xconf and configuring which fonts are available to Apache FOP and would like to use a relative path rather than an absolute path:

<fop version="1.0">

    <strict-configuration>true</strict-configuration>
    <strict-validation>true</strict-validation>
    <base>./</base>
    <font-base>./</font-base>
    <source-resolution>72</source-resolution>
    <target-resolution>72</target-resolution>
    <default-page-settings height="11in" width="8.26in"/>

    <hyphenation-pattern lang="en" country="ZA">en_ZA</hyphenation-pattern>

    <renderers>
        <renderer mime="application/pdf">
        <fonts>
            <directory recursive="true">/Users/me/Code/company/project/src/main/resources/templates/fonts</directory>
            <substitutions>
                <substitution>
                    <from font-family="Helvetica" />
                    <to font-family="OpenSans" />
                </substitution>
            </substitutions>
        </fonts>
        </renderer>
   </renderers>    
</fop>

Is it possible to set that fonts directory inside renderers to use a relative path?

Or is possible to set it programmatically in which case I could make use of

this::class.java.getResource("/templates/fonts/MyFont.ttf")

to get the full path?

2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./fonts could not be found.
2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./templates/fonts could not be found.
2017-10-24 05:24:48,591 WARN  org.apache.fop.apps.FOUserAgent - The font directory ./resources/templates/fonts could not be found.

nested for loop even number print out

hello I am writing a code that will print even integers after the 10th number it will start on a new line so it will be 10 numbers per column and 10 rows. my code is printing all number but i want it to only print even numbers i tried to use an if statement but it does not seem to work please help. below is my code.

public static void mjj() {
    int s = 1;
    for (int i = 1; i <=10;i++) {
        for (int j = 1; j <=10;j++) {
            if (s % 2 == 0)
                System.out.print(s++ + " ");
        }
        System.out.println();
    }
}

output

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 
71 72 73 74 75 76 77 78 79 80 
81 82 83 84 85 86 87 88 89 90 
91 92 93 94 95 96 97 98 99 100 

i want it to print this

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40

and so on until the 10th row

How to indicate the position in a Java class within an if statement?

I would like some advice, I want to evaluate my position within a java class and from that run some code using an if statement.

This is part of my code in Android Studio

`

@Override
    public void onBindViewHolder(PictureViewHolder holder, final int position) {
        PictureListPizza pictureListPizza = pictureListPizzas.get(position);
        holder.product_name.setText(pictureListPizza.getProduct_name());
        holder.product_ingredients.setText(pictureListPizza.getProduct_ingredients());
        holder.product_price.setText(pictureListPizza.getProduct_price());
        Picasso.with(activity).load(pictureListPizza.getPicture()).into(holder.product_photo);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (PizzaActivity.class != null){
                    if(position == 0){
                        Intent intent = new Intent(activity, Pizza1Activity.class);
                        activity.startActivity(intent);
                    }
                    if(position == 1){
                        Intent intent = new Intent(activity, Pizza2Activity.class);
                        activity.startActivity(intent);
                    }
                    if(position == 2){
                        Intent intent = new Intent(activity, Pizza3Activity.class);
                        activity.startActivity(intent);
                    }
                }
            }
        });

    }

`

As you can see I am placing if (PizzaActivity. class! = null){} but I know it is wrong.

It’s for an android project.

I hope you can help me, thank you in advance.

Greetings.

How to create an ArrayList from a dialog box in JavaFX?

Hello having trouble creating an array from a dialog box. I am able to type in the dialog and print out index [0] of ArrayList list. But when I press Ok it restarts and replaces index [0] instead of adding to the array list. How do I get the application to keep running instead of restarting?

Original Assignment: Going back to the GUI assignment. You can modify this to use JavaFX if you want to.
Create an application that has a button that has “Enter your info.” When clicked it will present a JDialog that has labels and text boxes that allow the user to enter their name, email, and phone number. The JDialog will have buttons OK and Cancel.
When the user clicks Cancel, the dialog will go away without doing anything else.
When the user clicks OK, the user’s information will be extracted from the dialog, and dumped out on the console.
Notice that you only are listening for Click events.
We will add to that spec.
Define a class to hold the info for a single person. Declare an ArrayList of this type. When the user clicks OK, extract the info from the dialog. This time, pass the info to the constructor method of your class that will hold the info. Add this newly created object to your array list.
Using an enhanced for loop, dump the ArrayList onto the console.
Add several user info’s to the ArrayList, each time, dumping the entire list to the console.
Sort the ArrayList. Dump the ArrayList onto the console.

public class UsefulGUI extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button();
        button.setText("Enter your info");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                dialogBox();
            }
        });

        StackPane pane = new StackPane();
        pane.getChildren().add(button);

        Scene scene = new Scene(pane, 500, 300);

        primaryStage.setTitle("Useful GUI");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public class User {
        private String name;
        private String email;
        private String phone;

        public User(String n, String e, String p) {
            this.name = n;
            this.email = e;
            this.phone = p;
        }
        @Override
        public String toString() {
            return "Name: " + name + " Email: " + email + " Phone: " + phone;
        }
    }
    public void dialogBox() {
        Dialog<User> dialog = new Dialog<>();
        ArrayList<User>list = new ArrayList<User>();


        dialog.setTitle("User info");

        Label name = new Label("Name: ");
        Label email = new Label("Email: ");
        Label phone = new Label("Phone: ");
        TextField textName = new TextField();
        TextField textEmail = new TextField();
        TextField textPhone = new TextField();

        GridPane grid = new GridPane();
        grid.add(name, 1, 1);
        grid.add(textName, 2, 1);
        grid.add(email, 1, 2);
        grid.add(textEmail, 2, 2);
        grid.add(phone, 1, 3);
        grid.add(textPhone, 2, 3);
        dialog.getDialogPane().setContent(grid);

        ButtonType buttonType = new ButtonType("Ok", ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(buttonType, ButtonType.CANCEL);

        dialog.setResultConverter(new Callback<ButtonType, User>() {
            @Override
            public User call(ButtonType b) {
                if (b == buttonType) {
                    return new User(textName.getText(), textEmail.getText(), textPhone.getText());
                }
                return null;
            }
        });
        Optional<User>info = dialog.showAndWait();

        // Adds user to list
        list.add(new User(textName.getText(), textEmail.getText(), textPhone.getText()));
        System.out.print("Info Added.n NEWUSER ");
        System.out.println(new User(textName.getText(), textEmail.getText(), textPhone.getText()));

        //Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(i + ". " + list.get(i));
        }

        //if (info.isPresent()) {
        //  System.out.println(info.get());
        //}
    }
    public void quitApplication() {
        Platform.exit();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

How to package a swt-javafx application in maven

My maven is like the following, while I can’t build a runnable jar by “mvn clean install”. The error throw when I run the jar is that I cannot find the classes in jfxswt.jar or jfxrt.jar. It seems that the lib of javafx is still can’t find in runtime.
Do someome have
same issue before?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>org.zhxie</groupId>
          <artifactId>org.zhxie.javaFxRateMeter</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <build>
            <resources>
              <resource>
                <directory>src/main/java</directory>
                <includes>
                  <include>**/*.fxml</include>
                </includes>
              </resource>
            </resources>
          </build>
      <profiles>
        <profile>
          <id>Windows-dev</id>
          <activation>
            <activeByDefault>true</activeByDefault>
          </activation>
          <dependencies>
            <dependency>
              <groupId>org.eclipse.swt</groupId>
              <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
              <version>4.3</version>
            </dependency>
            <dependency>
              <groupId>net.sf.opencsv</groupId>
              <artifactId>opencsv</artifactId>
              <version>2.3</version>
            </dependency>
            <dependency>
              <groupId>com.google.guava</groupId>
              <artifactId>guava</artifactId>
              <version>23.0</version>
            </dependency>
            <dependency>
              <groupId>org.javafx</groupId>
              <artifactId>jfxswt</artifactId>
              <version>1.0</version>
              <scope>system</scope>
              <systemPath>${project.basedir}libjfxswt.jar</systemPath>
            </dependency>
            <dependency>
              <groupId>org.javafx</groupId>
              <artifactId>jfxrt</artifactId>
              <version>1.0</version>
              <scope>system</scope>
              <systemPath>${project.basedir}libjfxrt.jar</systemPath>
            </dependency>
          </dependencies>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                  <excludeTransitive>false</excludeTransitive>
                  <stripVersion>false</stripVersion>
                </configuration>
                <executions>
                  <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                      <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                      <outputDirectory>${project.build.directory}/lib</outputDirectory>
                      <excludeTransitive>false</excludeTransitive>
                      <stripVersion>false</stripVersion>
                    </configuration>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
                </configuration>
              </plugin>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <classpathPrefix>lib/</classpathPrefix>
                      <mainClass>org.zhxie.main.MyFXCanvasTest</mainClass>
                    </manifest>
                    <manifestEntries>
                      <Class-Path>lib/jfxswt.jar,lib/jfxrt.jar</Class-Path>
                    </manifestEntries>
                  </archive>
                </configuration>
              </plugin>
            </plugins>
          </build>
        </profile>
      </profiles>
    </project>

………………….

Write method countVowels to take a string and return the amount of vowels

 public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input the string: ");
        String str = in.nextLine(); 

        System.out.print("Number of  Vowels in the string: " + countVowels(str)+"n");
    }

   public String countVowels(String count) {

}

sorry but im very new to java and coding and trying to find a way to create a vowel counter but I seem to struggle with creating one ive tried looking up many answers but cant find one.

TwinPrimes on JAVA

Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, 5 and
7 are twin primes, and 11 and 13 are twin primes. Write a Java program TwinPrimes.java that prompts
the user to input the search range of twin primes, display all the twin primes (2 pairs per line) within
the range, and print the number of twin primes found. The search range is assumed to be positive and
your program should repeatedly perform the same task until a sentinel value of -1 is entered.

The expected output of your program should be as follows:

Round 1:
Enter the search range: 100

(3,5) (5,7)

(11,13) (17,19)

(29,31) (41,43)

(59,61) (71,73)

Number of twin primes less than or equal to 100 is 8

Round 2:
Enter the search range: 150

(3,5) (5,7)

(11,13) (17,19)

(29,31) (41,43)

(59,61) (71,73)

……(Omitted)

Number of twin primes less than or equal to 200 is 15

Round 4:

Enter the search range: -1

End

I know that I am not complected the code, but I am struggling on how to print the Prime numbers in ( , ) ( , )way and how to calculate the number of twin primes show it at the end.

The below coding is what I had to do:

import java.util.Scanner;

import java.util.Random;

public class TwinPrimes {

    public static void main(String[] args) {

        int i = 0;

        int A, B = 0, D = 0;

        int num = 0;

        System.out.println("Round" + " " + ++i + ":");
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter the search range:");
        A = scn.nextInt();
        {
            if (A < 0)
                System.out.println("End");
        }

        for (i = 3; i <= A; i++) {

            int counter = 0;
            for (num = B; num >= 1; num--) {

                {
                    if (B % num == 0) {
                        counter = counter + 1;
                    }
                }
                if (counter == 2) {

                }
            }
            System.out.println("(" + i + "," + i + ")" + " " + "(" + i + "," + i + ")");

            // sum Number of twin primes to
            System.out.println("Number of twin primes less than or equal to " + A + " " + "is" + " ");
            return;
        }
    }
}