Why is the Ternary operator not working inside a method argument in java

I got this noted in a middle of a development.

Why is the Ternary operator not working inside a method argument? Here it is clearly InputStream or (else) String.

class A{

    public  static boolean opAlpha(InputStream inputStream) {

        // Do something

        return true;

    }

    public static boolean opAlpha(String arg) {

        // Do something else

        return true;
    }

    public static void main(String[] args) throws Exception {

        boolean useIsr = true;

        InputStream inputStream = null;
        String arg = null;

//      boolean isTrue = useIsr ? A.opAlpha(inputStream): A.opAlpha(arg); // This is OK.
        boolean isTrue =  A.opAlpha(useIsr ? inputStream : arg); // This is not. (Error : The method opAlpha(InputStream) in the type A is not applicable for the arguments (Object))

    }

}

JFrame won’t open when running application in Eclipse

I’m trying to create a simple GUI which takes two values and either subtracts/adds/divides dependant on which button the user selects. I’m comfortable with my code, but for some reason when I run my program, I’m not getting any visible frame, and I am also not getting any errors. Any help would be appreciated, Thanks!

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calc {

private JFrame frame;
private JTextField firstNumber;
private JTextField secondNumber;
private JTextField textField_2;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Calc window = new Calc();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Calc() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setTitle("Simple Calculator");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    firstNumber = new JTextField();
    firstNumber.setBounds(28, 41, 156, 26);
    frame.getContentPane().add(firstNumber);
    firstNumber.setColumns(10);

    secondNumber = new JTextField();
    secondNumber.setBounds(28, 145, 156, 26);
    frame.getContentPane().add(secondNumber);
    secondNumber.setColumns(10);

    JLabel lblNewLabel = new JLabel("First Number");
    lblNewLabel.setBounds(28, 27, 110, 16);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Second Number");
    lblNewLabel_1.setBounds(28, 131, 110, 16);
    frame.getContentPane().add(lblNewLabel_1);

    JLabel answerLabel = new JLabel("ANSWER:");
    answerLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    answerLabel.setBounds(216, 46, 190, 16);
    frame.getContentPane().add(answerLabel);

    JButton plusButton = new JButton("Add");
    plusButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 + number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    plusButton.setToolTipText("Adds first from second number");
    plusButton.setBounds(28, 183, 117, 29);
    frame.getContentPane().add(plusButton);

    JButton subtractButton = new JButton("Subtract");
    subtractButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 - number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    subtractButton.setToolTipText("Subtracts first from second number");
    subtractButton.setBounds(157, 183, 117, 29);
    frame.getContentPane().add(subtractButton);

    JButton divideButton = new JButton("Divide");
    divideButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{
                int number1 = Integer.parseInt(firstNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad first number.");
                return;
            }
            try{
                int number2 = Integer.parseInt(secondNumber.getText());
            }
            catch (Exception exc){
                JOptionPane.showMessageDialog(frame,"Error: bad second number.");
                return;
            }
            int number1 = 0, number2 = 0;
            int answer = number1 / number2;
            answerLabel.setText("The answer is: "+answer);
        }
    });
    divideButton.setToolTipText("Divides first from second number");
    divideButton.setBounds(289, 183, 117, 29);
    frame.getContentPane().add(divideButton);

    textField_2 = new JTextField();
    textField_2.setBounds(216, 94, 130, 26);
    frame.getContentPane().add(textField_2);
    textField_2.setColumns(10);
}
}

Why is my quicksort algorithm giving me out of bounds exceptions?

I’m trying to implement quicksort using the median of three algo and sometimes I can get my code to work, other times I get array out of bounds exceptions. One of my tests, that tests partition fails, as does a test on a sorted quicksort.

My code is:

public class QuickSort {

    static void swap(int[] A, int i, int j) {
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }

static final int LIMIT = 15;

static int partition(int[] a, int left, int right){
    int center = (left + right) / 2;
    if(a[center] < (a[left]))
        swap(a,left,center);
    if(a[right] < a[left])
        swap(a,left,right);
    if(a[right] < a[center])
        swap(a,center,right);

    swap(a,center,right - 1);
    return a[right - 1];
}

static void quicksort(int[] a, int left, int right){
    if(left + LIMIT <= right){
        int pivot = partition(a,left,right);

        int i = left;
        int j = right - 1;

        for(;;){
            while(a[++i] < pivot){}
            while(a[--j] > pivot){}
            if(i < j)
                swap(a,i,j);
            else
                break;
        }
        swap(a,i,right - 1);
        quicksort(a,left,i-1);
        quicksort(a,i+1,right);
    }
    else{
        insertionSort(a);
    }
}

public static void insertionSort(int[] a){
    int j;

    for(int p = 1; p < a.length; p++){
        int tmp = a[p];
        for(j = p; j > 0 && tmp < a[j-1]; j--)
            a[j] = a[j-1];
        a[j] = tmp;
    }
}

}

Program Design for displaying figures

The top picture is the direction for the program. The second picture is what the program should look like once executed. Right below that is my program. I got it to print the Box, X and the diamond. I need to code to make it look like the example. Im really stuck here on how to put this into the way the example shows.

Program

example

import java.util.Scanner;
public class Figures
{
//declare and initialize instance variable
public int size = 0;
//method printBox
public void printBox()
		{
		for(int i = 0; i < size ; i++)
		{
		for(int j = 0; j < size ; j++)
		{
		System.out.print("*");
		}
		System.out.println();
		}
		}
public void printX()
{
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
if (j == i) {
System.out.print("*");
} else if (j == size - (i - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println(); }
}
public void printDiamond()
{
Scanner in = new Scanner (System.in);
//System.out.print("Enter Number: ");
int inp = in.nextInt();
int wan = 1;
int space = inp ;
for(int i = 0; i <inp ; i++)
{
for(int j = 0; j < space; j++)
{
System.out.print(" ");
}
for(int j = 0; j < wan ; j++)
{
if(j == 0 || j == wan -1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
if(i == inp-1)
{
break;
}
else{
System.out.println();
wan=wan+2;
space--;
}
}
//second
int x = (inp * 2) - 1;
int y = 1;
for(int l = 0; l < inp; l++)
{
for(int j = y; j >= 1; j--)
{
System.out.print(" ");
}
for(int j = 0; j < x ; j++)
{
if(l >= 1)
{
if(j == 0 || j == x - 1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
}
System.out.println();
x-=2;
y++; }
}
  
public static void main(String[] args)
{ int num = 9;
Scanner scan = new Scanner(System.in);
  
  
while(num > 0 && num < 20)
{System.out.print("Enter a odd number between (0 to 9): ");
num= scan.nextInt();
  
if( num % 2 == 0)
{
System.out.println("Error. Please enter an odd number");
System.out.print("Enter a number between (1 to 9): ");
num= scan.nextInt();
}
else
{
System.out.println("You Enter: " + num);
}
//create an object for Box class
Figures myBox = new Figures();
Figures myX = new Figures();
Figures myDiamond = new Figures();
//change the size to 5 using '.' operator
myBox.size = num;
myX.size = num;
myDiamond.size = num;
//invoke the printBox method
myBox.printBox();
myX.printX();
myDiamond.printDiamond();
}
}}

Scanner Methods

I am trying to write a program that will use Scanner to view an imported file and add the “comparables” insides into a Binary Search Tree. However, I do not know what type of value will be passed in the file (Integer, String, etc.). Therefore, I cannot use the methods: .next() or .nextInt() when traversing the file. Is there a method that will alow me to pass any object inside the file?

Finding user input in the string & print in left-right order the word found in the string

I’m currently working on a console program, searching for words in String compare from String text, where those in String compare that are in between quotes or single quotes must be in the correct order found in text otherwise order is irrelevant. Words can be skipped or repeated must still be in a left to right order.

I’m having a problem with thinking how I will do the condition. Thank you!

Can’t Insert a element at the end of the linked list(singly)

i have written a function to insert a node after a specified node in a linked list (KEY) but somehow the function doesn’t work around the right boundary condition i.e i can’t insert a element by specifying the element as last element of the linked list.
Here is the Code.

/* This method contains some bugs, it can't add any elements at the end of linked list*/

    Node insertAfterNode(Node head, int data, int key) {
        if (head == null) {
            System.out.println("The list is empty.The key doesn't exist");
        } else {
            Node current = new Node();
           current = head;
            while (current != null) {
                if (current.data == key) {
                    Node newNode = new Node(data);
                    current.next = newNode;
                    newNode.next = next;
                } else {
                    current = current.next;
                    next = current.next;
                }
            }
        }
        return head;
    }

How to Use both Scala and Python in a Java project?

Is that possible a Java project act as network service by using Scala
and Python?

Because I need Scala to providing some functional level programming support and Python library to do some calculation on my system level data for my network service application based on Java.

Is there a way to mix/integrate them both Scala and Python with the Java application over HPC EC2 Instance?

I would greatly appreciate If someone provide me a helpful link or simple example with Java main method or hints to try from my-side.

Finding the possible combinations of a two dimensional array

I’m stuck on the following problem using Java. I will include my code at the bottom of this post. I get most of the combinations however I can’t figure out a way to get my column variable to be 0 for the remainder of my recursive calls which would help me in getting all combinations. The solution must work for two dimensional arrays of all sizes. Preferably I would like the solution to be entirely recursion – no loops. Thank you for any insight you may provide.

Given the definition of a 2D array such as follows:

String[][] data = {
 {"A", "B"},
 {"1", "2"},
 {"XX","YY","ZZ"}
};

Write a recursive program that outputs all combinations of each subarray in order. In the previous example, the desired output might look like the following:

A 1 XX
A 1 YY
A 1 ZZ
A 2 XX
A 2 YY
A 2 ZZ
B 1 XX
B 1 YY
B 1 ZZ
B 2 XX
B 2 YY
B 2 ZZ

Your program should work with arbitrarily sized arrays in either dimension. For instance, consider the following input array:

String[][] data = {
 {"A"},
 {"1"},
 {"2"},
 {"XX","YY"}
};

Should output:

A 1 2 YY
A 1 2 YY

My solution so far:

private String[][] data = {
        {"A", "B"},
        {"1", "2"},
        {"XX","YY","ZZ"}
};

public void combinations() {
    helperMethod("",0, 0);
}

private void helperMethod(String oldCombination, int row, int col) {
    String newCombination = oldCombination + data[row][col];

    if (row == data.length - 1) {
        System.out.println(newCombination);
    }

    if (row < data.length - 1) {
        helperMethod(newCombination, row + 1, col);
    }

    if (col < data[row].length - 1) {
        helperMethod(oldCombination, row, col + 1);
    }
}

Declaring a variable from a string input (Java)

When I previously asked a question, it got many downvotes because I was not very specific. I will do my best to be clear this time. I have read questions with similar titles, but none have made it clear to me.

Basically, I have been assigned to make a fractional calculator for my AP comp sci class. The first checkpoint for this assignment is to be able to read fractions inputted by the user without the code crashing. For example, “1_1/2” “1/2” or just 1.

So far, I’ve gotten the following –

public static void identify() {
        Scanner console = new Scanner(System.in);
        String fraction = console.next();

        if(fraction.contains("_")) {
            int wholenumber = console.nextInt();
            int numerator = console.nextInt();
            int denomenator = console.nextInt();
            System.out.print(wholenumber + "_" + numerator + "/" + denomenator);
        }
        else if(fraction.contains("/")) {
            int numerator = console.nextInt();
            int denomenator = console.nextInt();
            System.out.print(numerator + "/" + denomenator);

        } 
        else {                                                                          
            System.out.print(fraction);
        }

        }

I thought it was working at first, but then I realized that if I inputted 1_1/2 or 1/2, the code would wait for inputs for the numerator, denominator, and whole number. So my problem is that I want the previously mentioned values to be declared from the originally inputted string, and not from different inputs.

I understand this might be confusing to read, so basically
I if input 1_1/2, I want my code to declare 1 as wholenumber, 1 as numerator, and 2 as denominator. How would I achieve this?