LinuxQuestions.org
Visit the LQ Articles and Editorials section
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, 10:32 PM   #1
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Rep: Reputation: 15
Do/while loop and if statements not functioning properly w/ array...


I swear this is my last question tonight lol.

I've gotten lots of great help with my coursework..but I have one last question as to why when I execute my program, it prompts to enter a month as designated however once you enter the month..it stops, nothing else happens, nothing is outputted.

Here is my code..

Code:
// The "ArrayPractice2" class.
import java.io.*;
public class ArrayPractice2

{
    public static void main (String[] args)
    throws java.io.IOException
    {
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        
        String monthsArray [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        String input;
        String word;
        word = "stop";
        

        do
        {
            System.out.print ("Please enter a month name (or type 'stop' to exit): ");
            input = br.readLine();
            
            if (input == monthsArray[0])
            {
                System.out.println ("1");
            }
            else if (input == monthsArray[1])
            {
                System.out.println ("2");
            }
            else if (input == monthsArray[2])
            {
                System.out.println ("3");
            }
            else if (input == monthsArray[3])
            {
                System.out.println ("4");
            }
            else if (input == monthsArray[4])
            {
                System.out.println ("5");
            }
            else if (input == monthsArray[5])
            {
                System.out.println ("6");
            }
            else if (input == monthsArray[6])
            {
                System.out.println ("7");
            }
            else if (input == monthsArray[7])
            {
                System.out.println ("8");
            }
            else if (input == monthsArray[8])
            {
                System.out.println ("9");
            }
            else if (input == monthsArray[9])
            {
                System.out.println ("10");
            }



        }
        while (!word.equalsIgnoreCase ("stop"));
    } // main method
} // ArrayPractice2 class
I'm also not sure if I did the 'if/else if' statements right.

Thanks in advance for help =]
 
Old 05-01-2006, 10:42 PM   #2
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
You have initialized the string "word" to "stop". In the code what happens is even though the While is placed at the end of the DO loop it still checks the conditions before it enters the loop. What happens it immediately sees that word.equals("stop") is true so it never enters the loop.
Instead you should have word = new String(); before the loop, and in your loop you will want to set word=input; (which currently isn't in the code)
Then before it starts the loop again it will have the new information stored to word, and will make the correct comparison.
I also think in your if statements you will need to have input.equals(monthsArray[#]) because I do not think the string comparison with == will work. (You do this correctly in the while comparison, but not in the if comparisons)
I hope this helps, good luck with your class. What are you currently taking?
jdwilder
 
Old 05-01-2006, 10:45 PM   #3
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Well, it's a highschool course, 3U Java Programming. It wasn't my decision to be in the course, my dad's a programmer and he wants me to learn some as well to see if it's what I want to do, but it's pretty evident that It doens't make much sense to me. I'm sure that if I was better at maths, i'd do better because Java is like Algebra (something that completely escapes me).
 
Old 05-01-2006, 10:54 PM   #4
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
Well I hope the class isn't too hard on you. I have always been pretty good at math, but interestingly enough I had originally intended on getting a degree in Spanish linguistics. A teacher of mine used a programming language as a metaphor to help us learn Spanish. I ended up liking that course so much I decided to get a degree in computers instead.

Good luck!
 
Old 05-01-2006, 11:03 PM   #5
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
That's pretty rad.

I'm going to uni I think for psychology, english, and teaching...

I do graphic design, and php as well.

Thanks for the help =]
 
Old 05-02-2006, 02:22 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Well, iirc, the inventor of Perl, Larry Wall, is an arts grad :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script not functioning properly jordanmc31 Programming 21 04-17-2006 06:35 AM
My network is not functioning properly procfs Linux - Networking 5 02-16-2006 12:50 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
TCPIP not functioning properly safescott Linux - Networking 8 06-02-2003 01:13 AM
C++ problem , string.h not functioning properly cyberswami Programming 4 02-18-2003 11:20 PM


All times are GMT -5. The time now is 07:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration