File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need Help Stuck :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help Stuck :(" Watch "Need Help Stuck :(" New topic

Need Help Stuck :(

john don

Joined: May 01, 2008
Posts: 2
I am having trouble with nested loops i am kinda unsure how to do it. Help is really appreciated. Need to get this done today. I'm stuck on the inner loop not sure how to do what she is asking


import java.util.Scanner;

public class ProgrammingTest3 {
private Scanner reader = new Scanner(;

public static void main(String[] args) {
// Do NOT change this method
ProgrammingTest3 pt3 = new ProgrammingTest3();
int[] array = pt3.countDays();

* Count how many times the 13th occurs on a Sunday, Monday, ..., Saturday in a range
* of years entered by the user. This information is returned in the form of an array of ints.
* @return an array of ints of size 7. The first element holds the number of Sundays. The
* second element holds the number of Mondays and so on.
public int[] countDays() {
int[] days = new int[7]; //an int array. days[0] will hold how many Sundays,
//days[1] will hold how many Mondays, ...

int startYear, howManyYear;

startYear = getIntFromUser("Enter A Start Year. A positive Integer");

} while (startYear<1);

howManyYear = getIntFromUser("Enter How Many Years. A postive Integer");

} while (howManyYear<1);

* Declare two ints. One to hold the starting year. One to hold how many years.
* Call the method getIntFromUser until the user enters a positive int for the
* starting year. Notice that getIntFromUser accepts a String as a parameter.
* The String will be used by the getIntFromUser method to prompt the user. It
* should be a different prompt for the two different ints you will request.
* Notice that getIntFromUser returns an int that might be negative.
* This means you need to use a loop and call getIntFromUser until the user enters
* an appropriate int for the starting year (a positive int).
* Call the method getIntFromUser until the user enters a positive int for how
* many years. Another loop, very similar to above.

* Write nested for loops. That means one loop inside the other.
* The outer loop should vary the year starting from the year entered by the user.
* The outer loop should execute once for each year. The user told you how many years.
* The inner loop should vary the months from 1 (January) to 12 (December).
* Inside the inner loop, call the method whichDayOfWeek and send in the month and year.
* The whichDayOfWeek method will return an int 0-6. Use this as an index into the days
* array and increment that element.
for(int i=0; i<startYear; i++){


return days; //this returns the array of ints with all the information

* Determines which day of the week the 13th occurs on for a given month and year.
* @param month the month, a number from 1 to 12
* @param year the year, a positive number
* @return 0 for Sunday, 1 for Monday, 2 for Tuesday, ...
public int whichDayOfWeek(int month, int year){
int dayOfWeek=0;

* Just like ProgrammingTest1. Note the day is always 13.

return dayOfWeek;

* Prints out the final count of all the weekdays held in the parameter int array
* @param d the int array with the information of week days.
public void printDays(int[] d) {
* Prints out the array and the sum. The sum should be 12 times the number of years.
* Your printout must have a format similar to mine.

* Determines the maximum from an int array. The first element of the array represents
* the number of Sunday the 13ths, the 2nd element represents the number of Monday the
* 13ths, ...
* @param d the array containing the day of the week information.
public void whichIsMax(int[] d) {
* This method determines the maximum of the array d and remembers which index it was
* found at. If two or more indices tie, just remember the last one.
* Your printout must have a format similar to mine.
* You must use a switch statement to demonstrate you know how.

* This method will prompt the user for an int until the user types in an int. This method
* can return negative ints, 0, or positive ints. If the user types in a non-int, i.e. a String
* or a double, this method prompts them to try again until they get it right.
* @param prompt The prompt given to the user
* @return Returns the int entered by the user
public int getIntFromUser(String prompt) {
int num=0;
String s;
Scanner userInt = new Scanner(;

s = userInt.nextLine();
System.out.println("Hey, "+s+" is not a int. Please Enter a Integer");

num = userInt.nextInt();
System.out.println("You entered "+num);

* Use a Scanner object to read input from the keyboard.
* Prompt the user by printing the parameter variable prompt.
* If the user types in something other than an int, give them another chance
* until they type in an int.

return num;

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Welcome to the ranch. You would benefit greatly by reading the tips we compiled on how to get the most out of the Saloon: HowToAskQuestionsOnJavaRanch, in particular ShowSomeEffort, UseAMeaningfulSubjectLine, NotACodeMill and DoYourOwnHomework.

[My Blog]
All roads lead to JavaRanch
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11956

Your question isn't very clear... WHAT exactly are you stuck on?

a nested loop is exactly that... a loop inside a loop. in the simplest form, it would be

Where exactly are you stuck?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Need Help Stuck :(
It's not a secret anymore!