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 “ModuleNotFoundError: No module named ‘zlib'”?

Problem scenario
You are trying to install Python 3 but you get a message about zlib. This is the full message:

rm /usr/local/lib/python3.9/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.9/lib-dynload/__pycache__
/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.9.1
if test “xupgrade” != “xno” ; then \
case upgrade in \
upgrade) ensurepip=”–altinstall –upgrade” ;; \
install|*) ensurepip=”–altinstall” ;; \
esac; \
./python -E -m ensurepip \
$ensurepip –root=/ ; \
fi
Traceback (most recent call last):
File “<frozen zipimport”, …

How Do You Install Python 3.x on Any Type of Linux?

Problem scenario
You want a script to install Python 3.x that will work on any type of Linux. What should you do?

Solution
Prerequisite

Install make. If you need assistance, see this posting.

Install the zlib development tools. If you have a Red Hat derivative (e.g., CentOS or Fedora), you this command: sudo yum -y install zlib-devel

If you have a Debian/Ubuntu system,

How Do You Read a JSON File to Extract the Value of a Given Key in a JSON File?

Problem scenario
You want to print the value of a given non-nested key in a JSON value. How can Python help you achieve this?

Solution
Here is an illustrative example. Create a file called reader.py with the following lines of code:

import json
with open(“first.json”, “r”) as reading_content:
i = reading_content.read()
j = json.loads(i)
print(j[“bird”])

Have the JSON file look like this:

cat first.json

{“bird”: {“name”: “singy”, …

How Do You Create a Class That Is an Iterator in Python?

Problem scenario
You want to implement an object that is an iterator in Python. What do you do to implement iterator behavior in the class?

Solution
Run this program as an example:

class Computation:

def __init__(self, max = 0):
self.max = max

def __iter__(self):
self.n = 0
return self

def __next__(self):
if self.n <= self.max:
product = 2 * self.n
self.n += 1
return product

objecta = Computation(4) # The number must be 3 or greater if you want to print something other than “None” with this program. …

How Do You Extract an Email Address from a CSV File?

Problem scenario
You have a CSV file with email addresses. You want to return only the email address — not lines that have an email address. What do you do?

Solution
Assuming the email addresses are in a file named email.txt, use this Python 3 program:

with open(’email.txt’, ‘r’) as a:
for b in a:
if ‘@’ in b: #operate only on lines with “@”
c = b.split() #split up words on line
for d in c: #iterate through characters of words
if ‘@’ in d: #if email address, …

How Do You Solve “env: ‘python’: Permission denied”?

Problem scenario
You are running a script and you receive env: “‘python’: Permission denied”

Solution
Run this: sudo ls -lhd $(which python)

Look at the permissions, file owner and group associated with the results. Consider the user that brings about the original error. You may need to change the permissions of the file.

Is the file link pointing to a directory?