We are suppose to write a calendar for a project. I’m using an array to keep track of how many days in a month there are and get a long list of numbers equal to the value of that array index. I then populate a String using a loop until that loop is equal to the value of that index.
The string is loaded into a textshape, which was given by our professor. I want to display each text line that is generated by substring() into one separate line. I tried to start at a higher index (30, 59) but it returns an error b/c my initial string isn’t long enough.
So what i want to do is make that long list of numbers into separate lines. A classmate of mine said to use substring. It does work, generally but will only give me the first line.
this is the method to generate my string.
public String toString() {
monthPlusOne = month + 1;
for (int i = 0; i < monthsArray[monthPlusOne]; i++) {
String temp;
//temp = "" + i;
subLine = subLine + (i+1) + " ";
}
totalLine = subLine;
String output = monthText + " " + year + " \n "
+ "Su Mo Tu We Th Fr Sa\n" + totalLine;
return output;
}
it feeds into this this method which origanlly had the calender in just number form with manual “\n” inserted into it. The string would split when it found those “\n”
public static void displayCalendar() {
String s = calendar.toString().substring(30);
// extract all of the lines in s
// split s on \n
String[] lines = s.split("\n");
int index = 0;
for (String line : lines) {
t[index].setText(line);
index++;
}
}
Get rid of all those substring() and split() calls and add
inside the loop in toString().