Page 3 of 3 FirstFirst 123
Results 41 to 46 of 46
  1. #41
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Unfortunatly I've read that tutorial about 50 times. Doesn't delve into adding subtracting parts of the array, I found some stuff in my book, but there again its vague and for whatever reason, every tutorial on the planet decides to use nothing but integers. Then every problem you run into barely touches an integer :).

    Like the book I have goes into the Array index out of bounds error, but its a 4 line description which I understand why its doing it, but thats it. Also every single example uses i++ so guess that is the way I'll go and it gets me out of that error and works.
    Last edited by SMHouston; 09-05-2009 at 01:02 AM.

  2. #42
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    I got the first part to work, works flawlessly thanks for all the help there guys.

    Now on my second part I'm trying to add (n) days, from the user and printing out the day it would be.

    For example if they entered 23 days it would go 23 days ahead from the day they entered as the current day which I have the code to do in my Day.class

    Java Code:
    public void addDays(int nDays)
        {
            int i;
            for(i = 0; i < 7; i++)
                if(weekDay.equals(weekDays[i]))
                   break;
    
            weekDay = weekDays[(i + nDays) % 7];
        }
    Java Code:
    System.out.println("How many days would you like to look forward?");
    		System.out.flush();
    			nDays = Integer.parseInt(keyboard.readLine());
    		System.out.println();
    			
    		yourDay.addDays(nDays);
    			System.out.println("It will be: " + ndays);
    Figured out the error, however if I try to set it to + weekday it prints out the day they initially entered. If i set it to ndays it prints out the number of days they entered and its not touching the formula
    Last edited by SMHouston; 09-05-2009 at 01:52 AM.

  3. #43
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok figured it out, had to rewrite a few things but you can chalk this one up to solved and move it down or whatever you guys do. Thanks a TON for all the help and advice from everyone, here is what I ended up with:

    Day.class
    Java Code:
    public class Day
    {
    	
    	private String nextDay;
    	private String previousDay;
       private String weekDay;
       private String[] weekDays = {"Sunday", "Monday", "Tuesday",
                                     "Wednesday", "Thursday", "Friday",
                                     "Saturday"};
                                     
    	public Day()
    	{
    		weekDay = "Sunday";
    	}
    
    	public Day(String weekDay)
    	{
    		this.weekDay = weekDay;
    	}
        
    	public void printDay()
    	{
       	System.out.print(toString());        
    	}
    
    	public String toString()
    	{
    		return "Today is: " + weekDay;
    	}
    	
    	public String nextDay()
    	{
    		int i;
            
    		for(i = 0; i < 7; i++)
    			if(weekDay.equals(weekDays[i]))
    				break;
    		return weekDays[(i + 1) % 7];
        }
    
        public String prevDay()
        {
    		int i;
    		
    		for(i = 0; i < 7; i++)
    			if(weekDay.equals(weekDays[i]))
    				break;
    		return weekDays[(i + 6) % 7];
    	 }
    
        public String addDays(int nDays)
        {
            int i;
            for(i = 0; i < 7; i++)
                if(weekDay.equals(weekDays[i]))
                   break;
    
            return weekDays[(i + nDays) % 7];
        }
    
    	public void setDay(String weekDay)
    	{
    		this.weekDay = weekDay;
    	}
    
    	public String getDay()
    	{
    		return weekDay;
    	}
    	 
    	public String getNextDay()
    	{
    		return nextDay;
    	}
    		
    	public String getPrevDay()
    	{
    		return previousDay;
    	}
    }
    Test Program:

    Java Code:
    //Program Test Weekdays
    
    import java.io.*;
    
    public class TestWeekdays
    {
    	static BufferedReader keyboard =
                    new BufferedReader(new InputStreamReader(System.in));
    	public static void main(String[] args) throws IOException
    	{
    		Day myDay = new Day("Sunday");
    		Day yourDay = new Day();	
    		
    		int nDays;
    		String weekDay;
    		String nextDay;
    		String previousDay;
    		String[] weekDays = {"Sunday", "Monday", "Tuesday",
                                     "Wednesday", "Thursday", "Friday",
                                     "Saturday"};
    		
    		System.out.println("Please enter the day of the week: ");
    		System.out.flush();
    			weekDay = keyboard.readLine();
    		System.out.println();
    		
    		yourDay.setDay(weekDay);
    			System.out.println("Today is: " + weekDay);
    		
    		yourDay.nextDay();
    			System.out.println("Tommorow is: " + yourDay.nextDay());
    		
    		yourDay.prevDay();
    			System.out.println("Yesterday was: " + yourDay.prevDay());
    			System.out.println();
    			
    		System.out.println("How many days would you like to look forward?");
    		System.out.flush();
    			nDays = Integer.parseInt(keyboard.readLine());
    		System.out.println();
    			
    		yourDay.addDays(nDays);
    			System.out.println("It will be: " + yourDay.addDays(nDays) + " in " + nDays + " day(s)");
    								
    	}
    }

  4. #44
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    as a rule of thumb, you should loop through an array using the array.length, not a magic number

  5. #45
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Will do, I'll play with it more now that I got it working, that way I know exactly where I went wrong until it works again.

  6. #46
    JavaVideos is offline Member
    Join Date
    Sep 2009
    Posts
    13
    Rep Power
    0

    Default

    Glad to hear you have it working now - well done.

    Now would be a good time to refactor your code to improve it and make it easier to read.

    Firstly there are a few variables you have created along the way that are no longer used so you can take them out.

    Secondly this piece of code is repeated several times:

    Java Code:
                    int i;
            
    		for(i = 0; i < 7; i++)
    			if(weekDay.equals(weekDays[i]))
    				break;
    If you have a piece of code like this repeated several times it is good practice move it into its own method, so it is only written once.

    This method could be called something like findDayPosition and return a integer which is the position of the day in the array.

    Finally take a look at getNextDay and getPrevDay, do they work? Are they even needed anymore ?

Page 3 of 3 FirstFirst 123

Similar Threads

  1. How Does One Get Started Learning Java?
    By Wataru in forum New To Java
    Replies: 29
    Last Post: 08-08-2009, 11:45 AM
  2. Just started with Java
    By javades in forum New To Java
    Replies: 2
    Last Post: 07-24-2008, 07:39 AM
  3. getting started
    By ash in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:31 AM
  4. Just getting started with java
    By DuceDuceExplorer in forum Introductions
    Replies: 4
    Last Post: 06-29-2008, 07:13 AM
  5. Getting Started
    By Doorsmaniac in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 04:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •