Results 1 to 13 of 13
  1. #1
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default convert string array to int

    I need some help please. I am trying to convert an arraylist :
    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]
    into an integer.

  2. #2
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  3. #3
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What error do you receive? Is it runtime or compile error, kindly post the complete error message.

  5. #5
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    incompatible javatypes
    required: java.lang.string[]
    found: int

  6. #6
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Is this an assignment or what? Kindly show your lastest code so we are updated on the changes you made?
    Have you try Calendar?

  8. #8
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    I prefer not to pose it here
    Last edited by curioustoknow; 02-07-2011 at 07:27 AM.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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 08:06 AM.

  11. #11
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    you can close this thread now. I was able to fix my program. thank you all for the help

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    So what is your input?
    What is your expected output?
    Base on you last message the output is August 1, 2011

  13. #13
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    input is to enter an integer month (1 for jan, 2 for feb ...)
    and also enter a value of the day (1-31).
    the output should be something like: August 1, 2011 falls on a monday

Similar Threads

  1. convert byte array to string.
    By newbiejava in forum New To Java
    Replies: 9
    Last Post: 07-30-2010, 09:00 PM
  2. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 03:31 AM
  3. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 01:33 PM
  4. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 10:12 AM
  5. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM

Posting Permissions

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