LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2006, 09:26 PM   #1
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Rep: Reputation: 15
Arrays - User input - newbie = [


HI!

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. =]

Last edited by ohfaney; 05-01-2006 at 09:32 PM.
 
Old 05-01-2006, 09:51 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-01-2006, 09:54 PM   #3
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
So I can use an If statement? like if the user enters "1" it outputs pos 0? (january)..
 
Old 05-01-2006, 09:57 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Yes, inside the loop you can include an if, or a whole collection of them, one for each month!
 
Old 05-01-2006, 09:59 PM   #5
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
you said i need to change my for loop to one that supports a condition, would that be something like a while/dowhile? or am i just insane? lol...
 
Old 05-01-2006, 10:08 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-01-2006, 10:14 PM   #7
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
haha - yeah there's intervals where i sleep, but it makes sense now =] thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
Getting Input from User into Glibmm Kenji Miyamoto Programming 2 04-02-2006 06:12 PM
Newbie iptables INPUT question new@linux Linux - Security 6 03-08-2005 10:42 AM
validating user input blanks Programming 3 09-02-2004 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration