I’m working on (what is to a beginner) a rather complex assignment. I think I’ve got the jist of it down, but I’m having trouble with it printing “null” after Monday-Sunday are entered into the dialog box. Also, the goal of the assignment is, later, for the user to enter a number 0-6 and then the corresponding weekday (from the String[] weekArray set in the method) is printed. I’m not really sure how to do this and my book doesn’t seem to be showing me the way, but am I even going to be able to do that the way my code is set up? Thanks and best regards. Here’s the code.
EDIT – there’s an example in my book under returning an array from a method. I’m not sure if it’s applicable to my assignment as they seem to have different goals, but here it is..
EDIT#2 – instructions for reference to what I’m doing.
EDIT#3 – my interpretation of the example for passing methods to arrays.
EDIT #4 – solved the issue. I was going about it the wrong way for much. The book’s example wasn’t much help. Thanks to those of you who replied. I deleted the homework assignment description in consideration of the professor (who probably wouldn’t like his assignments on the internet next semester)
Excerpt of the code below… I was making this way more complicated than it was
public static String[] getWeek() {
String[] weekArray = new String[7];
for (int i = 0; i < weekArray.length; i++) {
weekArray[0] = JOptionPane.showInputDialog("Enter 'Monday'. ");
if (weekArray[0].compareTo("Monday") > 0) {
weekArray[0] = JOptionPane.showInputDialog("Enter 'Monday'. ");
Your branches have unreachable statements –
From String#equalsIgnoreCase:
Emphasis mine.
EDIT: From your edit above you appear to be having trouble with populating an array. The rough idea is:
This will print:
The idea is that what is passed to the method
populateArrayis a reference to the location of the array in memory. That means that you can manipulate the contents of the array, and the change will be reflected invaluesafterwards.