11 Replies - 1024 Views - Last Post: 01 January 2010 - 10:47 AM Rate Topic: -----

#1 liklsolja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-December 09

Arrays

Post icon  Posted 31 December 2009 - 09:11 AM

Hi all

I've been through the array tutorials on this forum several times, but still struggling with the code for this exercise. I need to write a code that accepts a integer parameter in a set range, in this case represented by days and I would like the result returned as the day corresponding to the parameter.

Can anyone help please,

The declaration for the array is as follows;

String [] day = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};




I Know I need to use ; public String getDayName(int dayNumber) {
return(day[dayNumber-1]);}

but I'm not sure how to put the code together.

Anyone, your help or advice would be greatly appreciated. :)

liklsoja

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Arrays

Posted 31 December 2009 - 09:55 AM

You've posted this in the wrong place. I'm going to guess this is C# and move it there.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Arrays

Posted 31 December 2009 - 10:00 AM

I would use the GetValue method of the array, something like this

public string GetValueFromArray(int index)
{
	string[] day = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	return day.GetValue(index).ToString();
}


Was This Post Helpful? 0
  • +
  • -

#4 liklsolja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-December 09

Re: Arrays

Posted 31 December 2009 - 10:21 AM

View PostPsychoCoder, on 31 Dec, 2009 - 09:00 AM, said:

I would use the GetValue method of the array, something like this

public string GetValueFromArray(int index)
{
	string[] day = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	return day.GetValue(index).ToString();
}




Hi and thanks for the help but can you Please move my topic back to where it originated as the problem posed is a java one. As it is Java I'm trying to learn.

Thanks again

liklsolja :)

p.s don't need the code just a better unstanding of what I am trying to achieve.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Arrays

Posted 31 December 2009 - 10:22 AM

I can do that. Forget the code then, it's C# code.

Moved back to Java
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Arrays

Posted 31 December 2009 - 10:27 AM

The topic was originally posted in Introduce Yourself, not Java. I thought it was C# so I moved it there.
Was This Post Helpful? 0
  • +
  • -

#7 liklsolja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-December 09

Re: Arrays

Post icon  Posted 01 January 2010 - 09:05 AM

Hi all

I've been through the array tutorials on this forum several times, but still struggling with the code for this exercise. I need to write a code that accepts a integer parameter in a set range, and I would like the result returned as the day corresponding to the parameter.

Can anyone help please,

The declaration for the array is as follows;

String [] day = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};




I Know I need to use ; public String getDayName(int dayNumber) {
return(day[dayNumber-1]);}

but I'm not sure how to put the code together.

Anyone, your help or advice would be greatly appreciated. :)

liklsoja
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Arrays

Posted 01 January 2010 - 10:12 AM

Quote

but I'm not sure how to put the code together.


There's not much to it - all you need to do is place the code you quoted before the return statement
Was This Post Helpful? 1
  • +
  • -

#9 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Arrays

Posted 01 January 2010 - 10:14 AM

As goose said, something like this:

public String getDayName(int dayNumber) 
{
String [] day = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

return(day[dayNumber-1]);
}



Good Luck :)
Was This Post Helpful? 0
  • +
  • -

#10 anonymouscodder   User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Arrays

Posted 01 January 2010 - 10:23 AM

And observe that if a value out of the range 1~7 is passed you will get an ArrayIndexOutOfBoundException.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Arrays

Posted 01 January 2010 - 10:33 AM

Please stop creating new threads for the same topic. The thread you opened yesterday on the subject that PsychoCoder and JackOfAllTrades were helping you in would have been a good place. Look- if your thread falls to the bottom of the page, you can always bump it. Thanks! :)

Link to the Original Thread:
http://www.dreaminco...topic147721.htm

Also, in terms of your problem, it would be more memory efficient to store the String[] containing the days as a global variable in your class, rather than as a local variable in your method. What will happen is that each time your method is invoked, the Array is created again. So to illustrate:
class YourClassNameHere{
   String [] day ={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

   public String getDayName(int dayNumber){
	   return day[dayNumber-1]; 
	} 
   ..other code..
}


This post has been edited by macosxnerd101: 01 January 2010 - 10:36 AM

Was This Post Helpful? 1
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Arrays

Posted 01 January 2010 - 10:47 AM

Quote

Also, in terms of your problem, it would be more memory efficient ...


That's certainly true. Better still, declare it as

public static final String [] day ={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1