Results 1 to 9 of 9
Thread: Help please
- 11-14-2010, 09:10 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Help please
Hello guys, I really need help, how I can convert String to Integer. I tryed but I still have problem, the result woudn't change. Basicly this code display clock with days but I got problem, it gives me number of days not name of days. How I can print days instead of number of days.
Java Code:package clockdriver; /** * * @author */ public class ClockDriver { /** * @param args the command line arguments */ public static void main(String[] args) { Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object System.out.println(MyClock); // Should display: Thursday, 12:00 p.m. System.out.println(); // print blank line MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes) System.out.println(MyClock); // Should display: Thursday, 2:30 p.m. System.out.println(); MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes) System.out.println(MyClock); // Should display: Friday, 12:15 a.m. System.out.println(); MyClock.advance(10080); // advance time by 10080 minutes (7 days) System.out.println(MyClock); // Should display: Friday, 12:15 a.m. System.out.println(); MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes) System.out.println(MyClock); // Should display: Thursday, 2:30 p.m. System.out.println(); MyClock.reverse(17280); // reverse time by 17280 minutes (12 days) System.out.println(MyClock); // Should display: Saturday, 2:30 p.m. System.out.println(); } // end of main } // end of class ClockDriver
Disply :Java Code:class Clock{ /*************************************************************** * Objects of this class represent time (in hours and minutes) * * The time data is stored in 24 hour (HHMM military format) * ***************************************************************/ private int day_of_week; // valid range of values is 1 thru' 7 private int hours; // range of values is 0 thru' 23 private int minutes; // range of values is 0 thru' 59 public Clock(String S, int h, int m){ day_of_week = Integer.parseInt( "5"); [COLOR="Red"](I have problem in this area, it still give me numbers of days)[/COLOR] hours=0; minutes=0; } public void advance(int m){ int totalminutes = (hours*60)+(minutes+m); hours = (totalminutes/60)%24; minutes=totalminutes%60; day_of_week=totalminutes%8; } public void reverse(int m){ int totalminutes = (hours*60)-(minutes-m); day_of_week=totalminutes%8; hours = (totalminutes%24)/60; minutes=totalminutes%60; } @Override public String toString(){ String s = + [COLOR="Red"]day_of_week[/COLOR] + ", " + [COLOR="Red"] I have problem with "day_of_week" always print numbers of days not name of the day.[/COLOR] (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " am" : " pm"); return(s); } }
I dont know how to disply this result: :(Java Code:run: [COLOR="Blue"]5, 12:00 pm 6, 2:30 pm 7, 12:15 am 7, 12:15 am 2, 12:30 pm 2, 12:30 pm[/COLOR]
Java Code:[COLOR="Blue"]Thursday, 12:00 pm Thursday, 2:30 pm Friday, 12:15 am Friday, 12:15 am Thursday, 2:30 pm Saturday, 2:30 pm[/COLOR]
Last edited by Albert050; 11-14-2010 at 05:48 PM. Reason: Moderator Edit: Code tags added
-
It's doing what you tell it to do. In your Clock toString method, you're telling it to print an int. One possible solution is to place a private static final String array in your Clock class that holds the names of the days of the week, and then in your toString method use the day_of_week value as the index to this array.
Edit: Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
I've added tags to your post above.Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Much luck!Last edited by Fubarable; 11-14-2010 at 12:11 PM.
- 11-14-2010, 06:51 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Is this what did you mean?
Result: if I did like this in class clockJava Code:class Clock{ /*************************************************************** * Objects of this class represent time (in hours and minutes) * * The time data is stored in 24 hour (HHMM military format) * ***************************************************************/ private int day_of_week; // valid range of values is 1 thru' 7 private int hours; // range of values is 0 thru' 23 private int minutes; // range of values is 0 thru' 59 private static final String[] array= {"Sunday","Monday","Tusday","Wensday","Thusday","Friday", "Saturday"}; public Clock(String S, int h, int m){ day_of_week = Integer.parseInt("5"); hours=0; minutes=0; } public void advance(int m){ int totalminutes = (hours*60)+(minutes+m); hours = (totalminutes/60)%24; minutes=totalminutes%60; day_of_week=totalminutes%8; } public void reverse(int m){ int totalminutes = (hours*60)-(minutes-m); day_of_week=totalminutes%8; hours = (totalminutes%24)/60; minutes=totalminutes%60; } @Override public String toString(){ String s =array + ", " + (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " am" : " pm"); return(s);
Java Code:private static final String[] array= {"Sunday","Monday","Tusday","Wensday","Thusday","Friday", "Saturday"}but if I deleted "[]" and {} and put one name one day of week from String in class clock like " private static final String array= "Sunday";Java Code:run: [Ljava.lang.String;@addbf1, 12:00 pm [Ljava.lang.String;@addbf1, 2:30 pm [Ljava.lang.String;@addbf1, 12:15 am [Ljava.lang.String;@addbf1, 12:15 am [Ljava.lang.String;@addbf1, 12:30 pm [Ljava.lang.String;@addbf1, 12:30 pm BUILD SUCCESSFUL (total time: 1 second)
result is
Java Code:run: Sunday, 12:00 pm Sunday, 2:30 pm Sunday, 12:15 am Sunday, 12:15 am Sunday, 12:30 pm Sunday, 12:30 pm BUILD SUCCESSFUL (total time: 0 seconds)
-
Your output is the toString representation of the entire array. Why not instead get the String held by the array that is associated with day_of_week by using day_of_week as an index of the array.
So this is the array:
array
but this is a String from the array, which is what you want:
array[3]
But instead of 3, you'll use day_of_week to choose the correct String. But day_of_week goes from 1 through 7, and the array will go from 0 to 6, so you'll want to use day_of_week - 1 as the index. Give it a try!
- 11-14-2010, 07:32 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
okay , I will try now
- 11-14-2010, 08:27 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
I really have hard time to do it. Can you give simple example like code close to the idea. I am new on Java. I learning.
- 11-14-2010, 09:31 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
how about this
Java Code:private static final String[] array={"Sunday","Monday","Tusday","Wensday","Thusday","Friday", "Saturday"}; public Clock(String S, int h, int m){ day_of_week = Integer.parseInt("7"); int day_of_week[]={0,1,2,3,4,5,6,7};
-
Here's a small example program of the concept:
Java Code:import java.util.Scanner; public class Fu1 { public static String[] colors = {"red", "blue", "green", "orange"}; public static void main(String[] args) { System.out.print("Please choose a number 1-4 for one of four colors, red, blue, green, or orange: "); Scanner input = new Scanner(System.in); int choice = input.nextInt(); input.nextLine(); // here I use the number selected to get the String out of the array System.out.println("You chose color " + colors[choice - 1]); } }
- 11-15-2010, 02:27 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Thank you so much, by your simple example I got what, I need. I only have now one problem.
output:Java Code:package clockdriver; /** * * @author */ public class ClockDriver { /** * @param args the command line arguments */ public static void main(String[] args) { Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object System.out.println(MyClock); // Should display: Thursday, 12:00 p.m. System.out.println(); // print blank line MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes) System.out.println(MyClock); // Should display: Thursday, 2:30 p.m. System.out.println(); MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes) System.out.println(MyClock); // Should display: Friday, 12:15 a.m. System.out.println(); MyClock.advance(10080); // advance time by 10080 minutes (7 days) System.out.println(MyClock); // Should display: Friday, 12:15 a.m. System.out.println(); MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes) System.out.println(MyClock); // Should display: Thursday, 2:30 p.m. System.out.println(); MyClock.reverse(17280); // reverse time by 17280 minutes (12 days) System.out.println(MyClock); // Should display: Saturday, 2:30 p.m. System.out.println(); } // end of main } // end of class ClockDriver class Clock{ /*************************************************************** * Objects of this class represent time (in hours and minutes) * * The time data is stored in 24 hour (HHMM military format) * ***************************************************************/ private int day_of_week; // valid range of values is 1 thru' 7 private int hours; // range of values is 0 thru' 23 private int minutes; // range of values is 0 thru' 59 private static final String[] Days={"Sunday","Monday","Tusday","Wensday","Thusday","Friday","Saturday"}; public Clock(String S, int h, int m){ day_of_week = Integer.parseInt("5"); hours=0; minutes=0; } public void advance(int m){ int totalminutes = (hours*60)+(minutes+m); hours = (totalminutes/60)%24; minutes=totalminutes%60; day_of_week=totalminutes%9; } public void reverse(int m){ int totalminutes = (hours*60)-(minutes-m); day_of_week=totalminutes%9; hours = (totalminutes%24)/60; minutes=totalminutes%60; } @Override public String toString(){ String s= Days[day_of_week - 1]+","+ (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " am" : " pm"); return(s); } }
I changed the percent that come with "day_of_week" and time but it didnt give me the same order like:Java Code:run: run: Thusday,12:00 pm Friday,2:30 pm Friday,12:15 am Friday,12:15 am Tusday,12:30 pm Friday,12:30 pm BUILD SUCCESSFUL (total time: 0 seconds) BUILD SUCCESSFUL (total time: 0 seconds)
Java Code:Thursday, 12:00 pm Thursday, 2:30 pm Friday, 12:15 am Friday, 12:15 am Thursday, 2:30 pm Saturday, 2:30 pm


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks