How Do You Pass an Array to a Method in Java?

Problem scenario
You want to pass an argument to another piece of code (e.g., a function in Java). How do you pass an array as a parameter to another portion of code?

Solution
Java only has methods — not functions (according to this posting).

This code (which should be called contInt.java) will pass a two-dimensional array called twoD to the method “coolp”.

How Do You Troubleshoot the Java Compilation Error “cannot find symbol”?

Problem scenario
You try to compile a Java program with a javac command. You get this error:

special.java:39: error: cannot find symbol
symbol: class foo
location: class bar

What should you do?

Possible solution
In some cases you need a keyword “new”, but in other cases you need to not have the keyword “new”.

How Do You Troubleshoot the Java Compilation Problem “error expected … illegal start of type”?

Problem scenario
When trying to compile a Java program, you get this output:

ContInt.java:15: error: expected
System.out.println(“test”);
^
ContInt.java:15: error: illegal start of type
System.out.println(“test”);
^

What do you do?

Solution
Create a class for the System.out.println statement to be in.

Here is code that would cause the error:

import java.util.Scanner; …

How Do You Troubleshoot “error: no suitable method found for toString(String)”?

Problem scenario
You are trying to print out a multi-dimensional array in Java. When you try to compile your program, you get this:

error: no suitable method found for toString(String)
System.out.println(Arrays.toString(Arrays.toString(var1)));
^
method Arrays.toString(long[]) is not applicable
(argument mismatch; String cannot be converted to long[])

What should you do?

Solution
You need to pass a one-dimensional array to Arrays.toString in the System.out.println statement.

How Do You Create a Callable Function in Java outside of the “main” Section?

Problem scenario
You have a public static void main (String[] args) portion of your Java program. You want to have a subroutine such as a function outside of this section. How do you code this?

Solution
In Java methods are components of an object that perform an action on the object themselves. We therefore use the term “method” here and not “function”.

How Do You Troubleshoot a pom.xml File?

Problem scenario
You are trying to run your Java program from a .jar file. You get an error like this:

Error: Could not find or load main class target.contint-0.1.0.jar
Caused by: java.lang.ClassNotFoundException: target.contint-0.1.0.jar

What should you do?

Solution
Examine the mainClass stanza. The word to the left of the period should be the name of the package.

How Do You Troubleshoot a Java Compilation Error “class fooBar is public, should be declared public class…”?

Problem scenario
You try to compile a java program, but you get this error:

hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
^
1 error

What is wrong?

Solution
The .java file must have a name of “HelloWorld” like the public class in the error.

How Do You Create a Puppet Manifest to Install Java?

Problem scenario
You have Puppet agent and Puppet Master set up and configured to work together. You are are running open source Puppet 5.x on Ubuntu servers in AWS. You want to install Java on the Puppet agent nodes. You tried to use the Java module.

On the Puppet Master server, you ran this: puppet module install puppetlabs-java –version 2.2.0

This is your site.pp file:

class { ‘java’ :
package =’java-1.8.0-openjdk-devel’, …

How Do You Write a Java Program to Read in Letters and Numbers?

Problem scenario
You want to create a basic Java program that can read alphanumeric input interactively. How do you write such a program?

Solution
1. Verify the “javac” command works. Run “man javac” to see the man page. If you do not get a man page, install the Java development tools. On a RedHat derivative, run this: sudo yum -y install java-devel

2.