ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I posted last night, regarding a problem with one of my programs for my coursework - It's all good and working now.
However, There is one program set that I don't understand, what I am being asked to do is create a program that allows the user to input the number of a month (ex. january is month 1)and outputted is the name of the month.
The program is to repeat until the user enters '-1'
My problem/question is! How would I do this? I understand a basic Array, with fixed Strings/integers.
Should I use two loops? Two Arrays? it's confusing :S Sorry, i'm new to all this and it takes me a while to understand things.
I started the program earlier today, and i'm also not sure if I was doing it even remotely right.
Code:
// The "ArrayPractice2" class.
public class ArrayPractice2
import java.io.*
{
public static void main (String[] args)
throws java.io.IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String monthsArray[] = new String [12];
String monthsArray[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String input;
int pos;
for (int i = 0; i < 12; i++)
{
monthsArray [i] = (int)
} // main method
} // ArrayPractice2 class
All help is appreciated and credited in my programs. =]
From your description I would suggest that what you want is a loop that will continue until a certain condition is met (namely the user enters -1) In that loop get the input from the user, if the number is -1 stop otherwise continue, if you are continuing you then want to check the number against the value in your month array.
So you first need to change your loop from a for loop to one that supports a condition
Next in the loop get the number from the user
Then display the month
As you write the code run and test it, but be aware that if you get it slightly wrong you program might end up in an infinite loop and so you will have to stop the program yourself.
That's right a while or do while would be the right one. I didn't want to say which one because I wanted to make sure that you were awake in your class
I really hope that helps, especially since I off now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.