Results 1 to 13 of 13
Thread: convert string array to int
- 02-07-2011, 05:32 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
convert string array to int
I need some help please. I am trying to convert an arraylist :
into an integer.Java Code:public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String Months[] = {"January","February","March","April","Main", "June", "July", "August", "September","October","November","December"}; System.out.print("Please enter an integer value for the month (e.g., 2 for February) : "); int monthvalue = keyboard.nextInt(); while (monthvalue<=0 || monthvalue>=13) { System.out.print("You have entered an invalid month entry, please enter a value from 1-12:"); monthvalue = keyboard.nextInt(); } if(monthvalue == 1) Months = Months[monthvalue-1]; [COLOR="Red"]// this gives me an error, how can I fix it?[/COLOR]
- 02-07-2011, 05:35 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
if you are trying to get the input from the user and convert that to an int then you can use:
Integer.parseInt(string);
Second, you are getting an error at the end because Months is an array, your left side of the statement is incorrect.
- 02-07-2011, 05:41 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
how can I fix my last statement to have it read a string. for example if user enter 1, it should read Month[0] which is January and so on
- 02-07-2011, 05:55 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What error do you receive? Is it runtime or compile error, kindly post the complete error message.
- 02-07-2011, 05:59 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
incompatible javatypes
required: java.lang.string[]
found: int
- 02-07-2011, 06:05 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
ok, I figure it out by using tostring method. now how can I force a check for each month, lets say the user enter 28 for january, it should show an error and only accept 31. and so on for the other months.
- 02-07-2011, 06:18 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Is this an assignment or what? Kindly show your lastest code so we are updated on the changes you made?
Have you try Calendar?
- 02-07-2011, 06:25 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
I prefer not to pose it here
Last edited by curioustoknow; 02-07-2011 at 06:27 AM.
- 02-07-2011, 06:25 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
or Date class? I am not an expert but you might need computation like, to get last day of febuary you need a date which is March 1, <chosen year> then minus 1 day so the result will be the last day of febuary
- 02-07-2011, 06:40 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Please do not send private message that is being discussed in a thread, there might be a better help for other members so every reply should be seen by every one.
The actual output is August 1, 2011 monday
What is your input?
And what is your expected output?
EDITED: I edited the reply and remove the code as respect to the OP's request.Last edited by mine0926; 02-07-2011 at 07:06 AM.
- 02-07-2011, 07:04 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
you can close this thread now. I was able to fix my program. thank you all for the help
- 02-07-2011, 07:10 AM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
So what is your input?
What is your expected output?
Base on you last message the output is August 1, 2011
- 02-07-2011, 08:32 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
convert byte array to string.
By newbiejava in forum New To JavaReplies: 9Last Post: 07-30-2010, 08:00 PM -
Convert a vector to a string array
By orchid in forum New To JavaReplies: 4Last Post: 02-24-2010, 02:31 AM -
How to convert string array into object in java
By kgkamaraj in forum New To JavaReplies: 4Last Post: 02-12-2010, 12:33 PM -
How to convert a string array into object in java
By kgkamaraj in forum Advanced JavaReplies: 2Last Post: 02-12-2010, 09:12 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks