Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: String array combined with if-then-else to output different text for different values

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String array combined with if-then-else to output different text for different values

    Howdy,
    I want to create a String type array of values, then select and print a value in the array based on a pre-determined value (door is open or closed) using if-then-else.
    Here is what I have
    class Door {
       public static void main(String[] JD) { 
     
          int door = 0; //1 is open, 0 is closed
     
          String[] word = {"open", "closed"}; 
     
          if (door = 1) {
              word = 0;
          } 
     
          else if (door = 0) {
              word = 1; 
          }
          System.out.println("Door = " + word);
       }
    }
    When I try to compile, it gives four incompatible types errors.
    It says I need to use boolean operators instead of int,
    and that
     word =0; word = 1;
    are in int type, but they should be in java.lang.String[] form.
    So, obviously boolean would instead of int for this, but I want to use int so that I can later make a more complex code with more variables.
    What is the proper format to input the "word" values? I have tried "0" and [0], neither of which work.
    This is pissin me off,
    any help will be greatly appreciated


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: String array combined with if-then-else to output different text for different va

    class Door {
       public static void main(String[] args) { 
     
          int door = 0; //1 is open, 0 is closed
     
          String[] word = {"open", "closed"}; 
     
          System.out.println("Door = " + word[door]);
       }
    }

Similar Threads

  1. Replies: 1
    Last Post: March 22nd, 2010, 04:34 PM
  2. Output String Handling in Java
    By merry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2010, 09:59 PM
  3. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM
  4. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM