#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Final Project help. PLEASE. Completely stuck after 6 hours. ARRAYS


    Hi all. I am on my final assignment for my intro level java course. I am completely lost and clueless. Our teacher never taught us how to do this, and I'm stuck. It was already due, I am trying to catch up. Basically I'm stuck on the first step, which is having my program read specific lines of a text file. I know how to run arguments, but I can't seem to have it read specific lines. Here is the code, and you can't imagine how thankful I'd be for any help. Thanks.

    Code:
          import java.util.*;
       import java.text.*;
    /** A "less than simple" date class;  an augmented and refactored version of SimpleDate by Anderson, Franceschi
    **	devloped by P.M.J. and R.W.M., October 28,  2012
    */
       public class SimpleDate {
          //Instance Variables
          private int month;
          private int day;
          private int year;
          
          //Default values
          private static final int MONTH = 1;
          private static final int DAY   = 1;
          private static final int YEAR  = 2000;
          
          //Public Constants (for the days of the week)
          public final int SUNDAY    = 1;
          public final int MONDAY    = 2;
          public final int TUESDAY   = 3;
          public final int WEDNESDAY = 4;
          public final int THURSDAY  = 5;
          public final int FRIDAY    = 6;
          public final int SATURDAY  = 7;
          
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       // C o n s t r u c t o r   M e t h o d s
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       
       /** Default constructor;
       *  Uses the default values for the new object.
       */
          public SimpleDate( ) {
             month = MONTH;
             day   = DAY;
             year  = YEAR;
          }
       
       /** Overloaded constructor;
       *  sets object's value to the given triplet
       *  @param mm    initial value for month
       *  @param dd    initial value for day
       *  @param yyyy  initial value for year
       */
          public SimpleDate(int mm, int dd, int yyyy) {
             if(!isValidDay(mm,dd,yyyy)) {
                mm   = MONTH;
                dd   = DAY;
                yyyy = YEAR;
             }
             setDate(mm,dd,yyyy);
          }
       
       /** Overloaded additional constructor; recognizes mm/dd/yyyy and mm-dd-yyyy forms
       *  @param date  expressed in either mm/dd/yyyy or mm-dd-yyyy form
       */
          public SimpleDate(String date) {
             setDate(date);
          }
       	
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       // A c c e s s o r / O b s e r v e r   M e t h o d s
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
          
          public int getMonth( ) { 
             return month; }
          public int getDay( )   { 
             return day; }
          public int getYear( )  { 
             return year; }
       
       /** Returns a value representing the day of the week; corresponding to the public constants, SUNDAY, 
       *  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY.
       */
          public int getDayOfWeek() {
             Calendar c = Calendar.getInstance(); 
             c.setTime(new Date((year-1900),(month-1),day));
             int dow = c.get(Calendar.DAY_OF_WEEK); //1=sunday, 2=monday, 3=wenesday, etc.
             return dow;
          }
       
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       // M u t a t o r   M e t h o d s
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       
       /** Sets the object's value to the given triplet. In the event the given date value is invalid, the 
       *  object is unchanged.
       *  @param mm    new value for month
       *  @param dd    new value for day
       *  @param yyyy  new value for year
       */
          public void setDate(int mm, int dd, int yyyy) {
             if(!isValidDay(mm,dd,yyyy)) {
                mm = MONTH;  dd = DAY;  yyyy = YEAR;
             }
             month = mm;  day = dd; year = yyyy;
          }
          
       /** Sets the object's value to that expressed in the given string; recognizes mm/dd/yyyy and mm-dd-yyyy forms.
       *  In the event the given date value is invalid, the object is unchanged.
       *  @param date  expressed in either mm/dd/yyyy or mm-dd-yyyy form
       */
          public void setDate(String date) {
             SimpleDate sd = parseDate(date);
             month = sd.getMonth();
             day   = sd.getDay();
             year  = sd.getYear();
          }
       	
       /** Sets the month value part of the object's value to the given value.  In the event the given month 
       *  value is invalid, the object is unchanged.  Note that the day value part of the resultant date value 
       *  may be adjusted earlier to represent a valid date. 
       *  @param mm new value for month
       */
          public void setMonth(int mm) {
             if(isValidMonth(mm)) {
                month = mm;
                rollBack();
             }
          }
       
       /** Sets the day value part of the object's value to the given value.  In the event the given day 
       *  value is invalid, the object is unchanged.
       *  @param dd new value for day
       */
          public void setDay(int dd) {
             if(isValidDay(month,dd,year)) {
                day = dd;
             }
          }
       
       /** Sets the year value part of the object's value to the given value.  Note that the day value part of 
       *  the resultant date value may be adjusted earlier to represent a valid date. 
       *  @param yyyy new value for year
       */
          public void setYear(int yyyy) {
             year = yyyy;
             rollBack();
          }
       
       //-----------------------------------------------------------------------------------------------------      
       
       /** Modifies the object's value to represent the day before the current value.
       */
          public void prevDay( ) {
             //CODE NEEDED HERE!!!
             day = day - 1;
             if (!isValidDay(month,day,year)) {
                month = month - 1;
                if (!isValidMonth(month)) {
                   month = 12;
                   year = year - 1;
                }
                day = monthDays(month,year);
             }
          }
          
       /** Modifies the object's value to represent the day after the current value.
       */
          public void nextDay( ) {
             day = day + 1;
             if (!isValidDay(month,day,year)) {
                day = 1;
                month = month + 1;
                if (!isValidMonth(month)) {
                   month = 1;
                   year = year + 1;
                }
             }
          }
          
       /** Modifies the object's value to represent the date correspondng to the 
          specified number of days before the current value.
       *  @param n the specified number of days to be used to modify the date value; Note n must be >= 0.
       */
          public void prevDay(int n) {
             //CODE NEEDED HERE!!!
             while(n > 0) {
                prevDay();
                n = n - 1;
             }
          }
          
       /** Modifies the object's value to represent the date corresponding to the
          specified number of days after the current value.
       *  @param n the specified number of days to be used to modify the date value; Note n must be >= 0.
       */
          public void nextDay(int n) {
             //CODE NEEDED HERE!!!
             while(n > 0) {
                nextDay();
                n = n - 1;
             }
          }
           
       //-----------------------------------------------------------------------------------------------------      
       
       /** Modifies the object's value to represent the date one week before the current value.
       */
          public void prevWeek( ) {
             //CODE NEEDED HERE!!!
             prevDay(7);
          }
          
       /** Modifies the object's value to represent the date one week after the current value.
       */
          public void nextWeek( ) {
             //CODE NEEDED HERE!!!
             nextDay(7);
          }
          
       /** Modifies the object's value to represent the date correspondng to the 
          specified number of weeks before the current value.
       *  @param n the specified number of weeks to be used to modify the date value; Note n must be >= 0.
       */
          public void prevWeek(int n) {
             //CODE NEEDED HERE!!!
             while(n > 0) {
                prevWeek();
                n = n - 1;
             }
          }
          
       /** Modifies the object's value to represent the date corresponding to the
          specified number of weeks after the current value.
       *  @param n the specified number of weeks to be used to modify the date value; Note n must be >= 0.
       */
          public void nextWeek(int n) {
             //CODE NEEDED HERE!!!
             while(n > 0) {
                nextWeek();
                n = n - 1;
             }
          }
           
       //-----------------------------------------------------------------------------------------------------      
       
       /** Modifies the object's value to represent the "corresponding date" in the preceeding month.
       */
          public void prevMonth( ) {
             prevMonth(1);
          }
          
       /** Modifies the object's value to represent the "corresponding date" in the following month.
       */
          public void nextMonth( ) {
             nextMonth(1);
          }
          
       /** Modifies the object's value to represent the "correspondng date" in the month specified by
       *  the given number of months before the current value.
       *  @param n the specified number of months to be used to modify the date value; Note n must be >= 0.
       */
          public void prevMonth(int n) {
             //CODE NEEDED HERE!!!
             if(month > n) {
                setMonth(month - n);
             } 
             else {
                year = year - (((n - month) / 12) + 1);
                n = n % 12;
                setMonth( (((((month - 1) - n) + 12) % 12) + 1));
             }
             rollBack();
          }
          
       /** Modifies the object's value to represent "corresponding date" in the month specified by 
       *  the given number of months after the current value.
       *  @param n the specified number of months to be used to modify the date value; Note n must be >= 0.
       */
          public void nextMonth(int n) {
             //CODE NEEDED HERE!!!
             year = year + ((month+n-1)/12);
             setMonth( ((((month - 1) + n) % 12) + 1));
             rollBack();
          }
         
       //-----------------------------------------------------------------------------------------------------      
       
       /** Modifies the object's value to represent the "corresponding date" one year before the current value.
       */
          public void prevYear( ) {
             prevYear(1);
          }
          
       /** Modifies the object's value to represent the "corresponding date" one year after the current value.
       */
          public void nextYear( ) {
             nextYear(1);
          }
          
       /** Modifies the object's value to represent the "correspondng date" the specified number of years 
       *  before the current value.
       *  @param n the specified number of years to be used to modify the date value; Note n must be >= 0.
       */
          public void prevYear(int n) {
              //CODE NEEDED HERE!!!
             setYear(year-n);
             rollBack();
          }
          
       /** Modifies the object's value to represent the "corresponding date" the specified number of years 
       *  after the current value.
       *  @param n the specified number of years to be used to modify the date value; Note n must be >= 0.
       */
          public void nextYear(int n) {
             //CODE NEEDED HERE!!!
             setYear(year+n);
             rollBack();
          }
           
       //-----------------------------------------------------------------------------------------------------      
       
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       // S t a n d a r d / C o n v e n t i o n a l   M e t h o d s
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       
       /** Returns a string representation of the object's current value.
       *  @return an expression of the date value in mm/dd/yyyy format (with 2 digit mm and dd)
       */
          public String toString( ) {
             String fmt = "%02d";
             DecimalFormat nf = new DecimalFormat("##");
             return String.format(fmt,month) + "/" + String.format(fmt,day) + "/" + year;
          }
       
       /** Compares this object to the given object for equality.
       *  @param   that, Object to compare to this object (required to be a SimpleDate object)
       *  @return  true if this object represents a date value equivalent to the value of the
       *  given object, false, otherwise.
       */
          public boolean equals(Object that) {
             boolean result = false;
             if (that instanceof SimpleDate) {   //Make sure that is a SimpleDate object
                SimpleDate date = (SimpleDate)that;
                result = (month == date.month) && (day == date.day) && (year == date.year);
             }
             return result;
          }
          
       /** Compares this object to the given object.
       *  @param   that, Object to compare to this object (required to be a SimpleDate object)
       *  @return  a positive integer if this object represents a date value later 
       *  in time than the value represented by the given object, a negative integer 
       *  if this represents a date earlier in time, and zero if the two objects 
       *  represent the same date value.
       */
          public int compareTo(Object that) {
             int result = 0;
             if (that instanceof SimpleDate) {   //Make sure that is a SimpleDate object
                //CODE NEEDED HERE!!!
                SimpleDate date = (SimpleDate)that;
                result = this.getYear() - date.getYear();
                if(result == 0) {
                   result = this.getMonth() - date.getMonth();
                   if(result == 0) {
                      result = this.getDay() - date.getDay();
                   }
                }
             }
             return result;
          }
          
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       // P r i v a t e   M e t h o d s
       ///////////////////////////////////////////////////////////////////////////////////////////////////////      
       
          /////////////////////////////////////////////////////////////////////////////////////////////////
          //Returns a SimpleDate object representing the date value expressed by the given string, or the
          //default value otherwise.  This method recognizes valid date values expressed in either the 
          //mm/dd/yyyy or mm-dd-yyyy forms.
          private SimpleDate parseDate(String date) {
             SimpleDate result = new SimpleDate();
             SimpleDate attempt;
             attempt = parseDate_mmddyyyy(date,'/');
             if(attempt != null) {
                result = attempt;
             }  
             else {
                attempt = parseDate_mmddyyyy(date,'-');
                if(attempt != null) {
                   result = attempt;
                } 
             }
             return result;
          }
                
          /////////////////////////////////////////////////////////////////////////////////////////////////
          //Returns a SimpleDate object representing the date value expressed by the given string, null 
          //otherwise.  delimiter serves to specify the particular character used to separate the date 
          //value parts; normally '/' or '-'.  This method can thus readily recognize valid date values
          //expressed in either the mm/dd/yyyy or mm-dd-yyyy form.
          private SimpleDate parseDate_mmddyyyy(String date, char delimiter) {
             SimpleDate result = null;
             int m, d, y;
             int index1 = date.indexOf(delimiter);
             int index2 = date.lastIndexOf(delimiter);
             if((index1 >= 0) && (index2 > index1)) {
                try {
                   m = Integer.parseInt(date.substring(0,index1));
                   d = Integer.parseInt(date.substring((index1+1),index2));
                   y = Integer.parseInt(date.substring(index2+1));
                   result = new SimpleDate(m,d,y);         
                }               
                   catch (NumberFormatException e)
                   {  //No remedial action necessary;  result will equal null
                   }
             } 
             return result;
          }
       
          /////////////////////////////////////////////////////////////////////////////////////////////////
          //Modifies the instance variable day, as necessary, until the triplet, month, day, year is valid 
          private void rollBack() {
             //CODE NEEDED HERE!!!
             if(!isValidDay(month,day,year)) {
                day = monthDays(month,year);
             }
    /*         while(!isValidDay(month,day,year)) {
                day = day - 1;
             }*/
          }
       
          //Returns true iff the given triplet corresponds to a valid date value
          /////////////////////////////////////////////////////////////////////////////////////////////////
          private boolean isValidDay(int month, int day, int year) {
             return isValidMonth(month) && 
                    ((day >= 1) && (day <= monthDays(month,year)));
          }
       
          //Returns true iff the given month corresponds to a valid month value
          /////////////////////////////////////////////////////////////////////////////////////////////////
          private boolean isValidMonth(int month) {
             return ((month >= 1) && (month <= 12));
          }
          
          //Returns the number of days in the given month and year
          /////////////////////////////////////////////////////////////////////////////////////////////////
          private int monthDays(int month, int year) {
             int result;
             //  Assert: (month >= 1) && (month <=12)
             if ((month==4) || (month==6) || (month==9) || (month==11)) {
                result = 30;
             } 
             else if(month == 2) {
                if (isLeapYear(year)) {
                   result = 29;
                } 
                else {
                   result = 28;
                }
             } 
             else { // (month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)
                result = 31;
             }
             return  result;
          }
          
          // Returns true iff the given year value qualifies as a "leap year".
          /////////////////////////////////////////////////////////////////////////////////////////////////
          private boolean isLeapYear(int year) {
             return !( ((year % 4) != 0) || ( ((year % 100) == 0) && ((year % 400) != 0) ) );
          }
       
       }
    That is the completed program he gave us. We need to make our own from scratch, and import specific lines of a text file (most common birthdays) to it. This is all I have so far.

    Code:
    	
    	import java.util.Scanner;
       import java.io.File;
       import java.io.IOException;
    
    	
    	public class MostCommonBirthday {
    	
    
    	 
         public static void main( String[] args) throws IOException {
          
         Scanner reader = new Scanner(args[0]);
    	  
    	
     
    
    		
    
    
    		
    		  
    		 
    	}
    	     static boolean equalsDayOfYear(SimpleDate one, SimpleDate two) {
                 return (one.getMonth() == two.getMonth()) &&
                          (one.getDay() == two.getDay());
            }
            // Returns a string representation of the given date value in the form mm/dd.
            static String toStringDayOfYear(SimpleDate sd) {
            return sd.toString().substring(0,5);
            }
       }

    Thanks sooooo much.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    I can't seem to have it read specific lines.
    What do you mean by "specific lines"?
    Do you mean a line with a specific content or substring? If so, read each line into a String and use the String class methods to test if the line contains the content.
    Or do you mean a line by relative position? Say the third line? If so, read and count the lines as they are read.

    If the lines in the file are EXACTLY the same length, the RandomAccessFile class could be used to read the line by indexing into the file to the location the line begins. For exampe if the lines are 10 bytes long, the second line starts at byte location 10.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    What do you mean by "specific lines"?
    Do you mean a line with a specific content or substring? If so, read each line into a String and use the String class methods to test if the line contains the content.
    Or do you mean a line by relative position? Say the third line? If so, read and count the lines as they are read.

    If the lines in the file are EXACTLY the same length, the RandomAccessFile class could be used to read the line by indexing into the file to the location the line begins. For exampe if the lines are 10 bytes long, the second line starts at byte location 10.
    My problem is that I don't know how to tell the program to make an array with the info that's in the text file. The program has to work with multiple different text files which is run as an argument. I don't know how i'd get the info from the text file into the code
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    how i'd get the info from the text file into the code
    What is in the file? Strings with spaces between?
    The Scanner class has methods for reading a file and separating it into separate Strings.
    Also the String class's split() method will separate a String into an array of Strings.

    It depends on what is in the file and where in the program you want to store it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    What is in the file? Strings with spaces between?
    The Scanner class has methods for reading a file and separating it into separate Strings.
    Also the String class's split() method will separate a String into an array of Strings.

    It depends on what is in the file and where in the program you want to store it.
    They are text files each of them are dates in the format dd/mm.

    Ex:
    08/18
    12/03
    04/22

    I need to take the text file and turn the dates into an array. The thing i'm having problems with is that it's not the same text file every time as in the program has to work for Birthdates.txt and Birthdates2.txt, etc, depending on which text file i type as the argument.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    What type of data in the array?
    You listed 3 lines of text? Would each line be read as a String and stored in an array of Strings?
    For example would the result look the same as this array:
    String[] anArray = {"08/18","12/03","04/22"};
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    What type of data in the array?
    You listed 3 lines of text? Would each line be read as a String and stored in an array of Strings?
    For example would the result look the same as this array:
    String[] anArray = {"08/18","12/03","04/22"};
    Yes, that is correct. It would be stored in an array of strings like that, but the dates are inside a text file. I dont know how to get it out of the text file
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Use the Scanner class's methods to read the contents of the file into Strings.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Use the Scanner class's methods to read the contents of the file into Strings.
    Could you give me an example of how you'd make a scanner for a filename that will be changed? Like how sometimes it will be Birthdates_actual.txt and sometimes it will be something else depending on which is set as the argument before it is run. All of the examples I have found for scanners you just type the text file name when youre making the scanner
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Look at the API doc for the Scanner class. It has examples for how to use it.

    If you have questions about any of the examples, copy the code for the example and paste it here and ask your question.
    Last edited by NormR; December 11th, 2012 at 07:01 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Look at the API doc for the Scanner class. It has examples for how to use it.

    If you have questions about any of the examples, copy the code for the example and paste it here and ask your question.
    I have read the API doc. It gives examples of scanners where a specific file is called ex: mynumbers.txt But as I said I need to make it so whatever I have typed in as the Argument to be run on jGrasp is the file that is scanned, I can't just type the file name because the program needs to work for different files not just one.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Use a String variable for to hold the filename of the file to be read.

    Then you can use many different techniques for assigning the filename value to the String.

    Those are two separate problems. You should work on them one at a time.
    1) read lines from a file whose name is in a String variable
    2) get the name of a file into a String variable

IMN logo majestic logo threadwatch logo seochat tools logo