Results 41 to 46 of 46
- 09-04-2009, 11:51 PM #41
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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 12:02 AM.
- 09-05-2009, 12:24 AM #42
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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]; }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 formulaJava 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);Last edited by SMHouston; 09-05-2009 at 12:52 AM.
- 09-05-2009, 01:15 AM #43
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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
Test Program: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; } }
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)"); } }
- 09-05-2009, 01:36 AM #44
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
as a rule of thumb, you should loop through an array using the array.length, not a magic number
- 09-05-2009, 03:40 AM #45
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
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.
- 09-05-2009, 09:55 AM #46
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
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:
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.Java Code:int i; for(i = 0; i < 7; i++) if(weekDay.equals(weekDays[i])) break;
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 ?
Similar Threads
-
How Does One Get Started Learning Java?
By Wataru in forum New To JavaReplies: 29Last Post: 08-08-2009, 10:45 AM -
Just started with Java
By javades in forum New To JavaReplies: 2Last Post: 07-24-2008, 06:39 AM -
getting started
By ash in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 10:31 AM -
Just getting started with java
By DuceDuceExplorer in forum IntroductionsReplies: 4Last Post: 06-29-2008, 06:13 AM -
Getting Started
By Doorsmaniac in forum Java AppletsReplies: 0Last Post: 11-24-2007, 03:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks