Some advice with some loops - I'm new to java!

I am trying to make a game (text based) Where you have 1 of 3 weapons a dog (can tell if a person is a vampire and warn you) a map (allows you to find a place to hide) and a gun (allows you to shoot the vampires!)
I want it to be a 50/50 chance of being a human or a vampire for now, so I have randomly generated either a 1 or 2 for vampire or human. The user will have chosebn his wepaon and also what he wishes to do...either run, hide or fight. Now I am trying to make a loop below:
public String test()
                    int d1 = (int) (Math.random() * 2) + 1;
                    if(weapon == "Dog")
                    //seePerson();
                    System.out.println("The dog can tell if its a vampire!");
                    else if (weapon == "Map")
                    //hide();
                    System.out.println("You can find a good place to hide!");
                    else if (weapon == "Gun")
                    //kill();
                    System.out.println("You can kill the vampire");
                    else if (weapon != "Dog" && d1 == '1')
                    System.out.println("This line wont work!");
                    System.out.println(d1); \\this one will????
                    return outputString;
               }

I have another problem...again possibly easily fixed.
                    int d1 = (int) (Math.random() * 2) + 1;
                    String weapon = "Gun";
                    if("Dog".equals(weapon))
                    //seePerson();
                    System.out.println("The dog can tell if its a vampire!");
                    else if ("Gun".equals(weapon) && (d1.equals('1')))
                    System.out.println(d1);
                    System.out.println("This line wont work!");
                    }When I try to compile it points at the line else if ("Gun".equals(weapon) && (d1.equals('1'))) and says that an int cannot be dereferenced. Can anyone tell me what is wrong with that, I dont think i have come across that before?
Thanks
Andy

Similar Messages

  • Help with set(index, object) please / new to java

    private static void setDobject(Rectangle [] setrect)
              LinkedList<Rectangle> rects = new LinkedList<Rectangle>();
              Rectangle myrectangle = new Rectangle(9.0,9.0);                                                                  
                 rects.set(1,myrectangle);
              for(Rectangle x : setrect)
                   System.out.print("Rectangle: ");
                   System.out.println(x.getLength() + " by " + x.getWidth());
              }  //End of for loop
         }  //End of unsorted
         }  the other previous code works, i have used the add(object) and it works here is the code for that:
    private static void addDobject(Rectangle [] addrect)
              LinkedList<Rectangle> rects = new LinkedList<Rectangle>();
              Rectangle myrectangle = new Rectangle(9.0,9.0);                                                                  
                 rects.add(myrectangle);
                 for(Rectangle x : rects)
                   System.out.print("Rectangle: ");
                   System.out.println(x.getLength() + " by " + x.getWidth());
              }  //End of for loop
              System.out.println();
         }  //End of unsorted
    this is the output error it is giving me :
    Part 1 : An Array List of Rectangles
    Enter length or 999 to exit: 3
    Enter width: 2
    Enter length or 999 to exit: 1
    Enter width: 2
    Enter length or 999 to exit: 10
    Enter width: 20
    Enter length or 999 to exit: 999
    Rectangle: 3.0 by 2.0
    Rectangle: 1.0 by 2.0
    Rectangle: 10.0 by 20.0
    Display Object with added item
    Rectangle: 3.0 by 2.0
    Rectangle: 1.0 by 2.0
    Rectangle: 10.0 by 20.0
    Rectangle: 9.0 by 9.0
    Displaying Objects with SET item
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size:
    0
    at java.util.LinkedList.entry(LinkedList.java:365)
    at java.util.LinkedList.set(LinkedList.java:328)
    at Lab11.setDobject(Lab11.java:129)
    at Lab11.main(Lab11.java:78)
    Press any key to continue...
    finally here is the Rectangle class i'm using:
    public class Rectangle
        private double length;     // Instance variables
        private double width;
        public Rectangle(double l, double w)  // Constructor method
            length = l;
            width = w;
        } // end Rectangle constructor
        public double getLength()             // getter 
             return length;
        } // end getLength
         public double getWidth()             // getter 
             return width;
        } // end getWidth
        public void setLength(double l)      // setter 
             length = l;
        } // end setLength
         public void setWidth(double w)     // setter 
             width = w;
        } // end setWidth
         public double calculateArea()         // calculation method
            return length * width;
        } // end calculateArea
        public void displayRectangle()         // display method
            System.out.println("Rectangle Length = " + length);
            System.out.println("Rectangle Width = " + width);               
        } // end displayRectangle
    } // Rectangle ClassHope you can help guys! thanks!

    LinkedList<Rectangle> rects = new LinkedList<Rectangle>();rects is an empty LinkedList at this point. It has no elements added to it.
    rects.set(1,myrectangle);So you can't set the item at index 1 (the 2nd element in the list) because it doesn't even exist. You need to add items to it.

  • Am unable to Install Adobe Flash, 2. I have a warning saying Jarva script void, could someone please give me some advice with these problems, I am new to Firefox and I am Trying to set up a new home page. Kind Regards, briansheree

    I have installed Firefox 8 and I am trying to set up a Homepage with downloads,Plugins and extension sidebar plus Bookmarks toolbar, with named folders containing my favorite sites, but when as instructed to upgrade Adobe Flash to the latest version the install fails. And I also had a warning in my search bar saying Java script void.

    I have installed Firefox 8 and I am trying to set up a Homepage with downloads,Plugins and extension sidebar plus Bookmarks toolbar, with named folders containing my favorite sites, but when as instructed to upgrade Adobe Flash to the latest version the install fails. And I also had a warning in my search bar saying Java script void.

  • I need some help with a loop..

    Hi,
    I am creating a program to calculate time, when entering the time and how many minutes you want to add. The program is done, except for the fact that I need to build a loop to add 1 to h (for hours) for every 60 minutes. My idea was to have the 'if-else' statement in newHours() loop. I tried to enclose this inside a 'do-while' loop, with while(newMin>60), but it seems that it creates an infinite loop as the program just sits there. If someone could check my code and gimme some advice I would reall appreciate it.
    Time.java
    import javax.swing.*;
    public class Time
         private String addMin;
         private String hours;
         private String minutes;
         public Time()
         public Time(String inHours, String inMinutes)
              hours = inHours;
              minutes = inMinutes;
         public void setTime(String inHours, String inMinutes, String addMinutes)
              setAddMin(addMinutes);
              setHours(inHours);
              setMinutes(inMinutes);
         public String getTime()
              return toString();
         public void setAddMin(String addMinutes)
              addMin = addMinutes;
         public String getAddMin()
              return addMin;
         public void setHours(String inHours)
              hours = inHours;
         public String getHours()
              return hours;
         public void setMinutes(String inMinutes)
              minutes = inMinutes;
         public String getMinutes()
              return minutes;
         public String newMinutes()
              int m, m2, newMin;
              m = Integer.parseInt(minutes);
              m2 = Integer.parseInt(addMin);
              newMin = m + m2;
              while(newMin>59)
                   newMin = newMin - 60;
              if(newMin>9)
                   return Integer.toString(newMin);
              else
                   return "0" + Integer.toString(newMin);
         public String newHours()
              int m, m2, h, newMin, temp;
              m = Integer.parseInt(minutes);
              m2 = Integer.parseInt(addMin);
              h = Integer.parseInt(hours);
              newMin = m + m2;
              if(newMin>59)
                   temp = newMin - 60;
                   h = h+1;
              else
                   h = h;
              return Integer.toString(h);
         public String convert()
              int h, h2;
              String day;
              h = Integer.parseInt(newHours());
              if(h>=12)
                   day = "PM";
                   h2 = h - 12;
                   hours = Integer.toString(h2);
              else if (h == 12)
              hours = Integer.toString(h);
              day = "PM";
              else
                   day = "AM";
                   hours = Integer.toString(h);
              return "your converted time is\n" + hours + ":" + newMinutes() + day;
         public void display()
              JOptionPane.showMessageDialog(null, this.convert());
    timeDriver.java
    import java.util.*;
    import javax.swing.*;
    public class timeDriver
         public static void main(String[] args)
              String input, hour, minute, addMin;
              int h, m;
              input = JOptionPane.showInputDialog("Enter time in 24-hour notation:");
              hour = input.substring(0,2);
              minute = input.substring(3, 5);
              addMin = JOptionPane.showInputDialog("How many minutes to add?");
              Time A = new Time();
              A.setTime(hour, minute, addMin);
              A.display();
    }

    Why do you think you need a loop at all?
    When you post code, wrap it in [code][/code] tags so it's easier to read.
    You realize that what you're writing basically duplicates the java.util.Calendar class (and its subclass GregorianCalendar), right?

  • Hi! i'm having a problem with my safari as unnecessary new tabs keeps opening overtime i open some link! is anyone experiencing something like this !

    hi! i'm having a problem with my safari as unnecessary new tabs keeps opening overtime i open some link! is anyone experiencing something like this !

    There is no need to download anything to solve this problem.
    You may have installed one or more of the common types of ad-injection malware. Follow the instructions on this Apple Support page to remove it. It's been reported that some variants of the "VSearch" malware block access to the page. If that happens, start in safe mode by holding down the shift key at the startup chime, then try again.
    Back up all data before making any changes.
    One of the steps in the article is to remove malicious Safari extensions. Do the equivalent in the Chrome and Firefox browsers, if you use either of those. If Safari crashes on launch, skip that step and come back to it after you've done everything else.
    If you don't find any of the files or extensions listed, or if removing them doesn't stop the ad injection, ask for further instructions.
    Make sure you don't repeat the mistake that led you to install the malware. It may have come from an Internet cesspit such as "Softonic" or "CNET Download." Never visit either of those sites again. You might also have downloaded it from an ad in a page on some other site. The ad would probably have included a large green button labeled "Download" or "Download Now" in white letters. The button is designed to confuse people who intend to download something else on the same page. If you ever download a file that isn't obviously what you expected, delete it immediately.
    Malware is also found on websites that traffic in pirated content such as video. If you, or anyone else who uses the computer, visit such sites and follow prompts to install software, you can expect more of the same, and worse, to follow. Never install any software that you downloaded from a bittorrent, or that was downloaded by someone else from an unknown source.
    In the Security & Privacy pane of System Preferences, select the General tab. The radio button marked Anywhere  should not be selected. If it is, click the lock icon to unlock the settings, then select one of the other buttons. After that, don't ignore a warning that you are about to run or install an application from an unknown developer.
    Still in System Preferences, open the App Store or Software Update pane and check the box marked
              Install system data files and security updates (OS X 10.10 or later)
    or
              Download updates automatically (OS X 10.9 or earlier)
    if it's not already checked.

  • New 2 Java with some errors in prog

    Hey Guys and Gals im new to java and im working on some simple progs
    my brain is currently wrapped around this problem
    ---------- Java Compiler ----------
    Register8.java:81: incompatible types
    found : char
    required: java.lang.String
              return bits.charAt(bitNumber);
    ^
    1 error
    Normal Termination
    Output completed (3 sec consumed).
    =======================CODE=======================
         public String getBit(int bitNumber)
              return bits.charAt(bitNumber);
    this is the calling stuff
    public class Driver
         public static void main(String[] args)
              Register8 Binary1 = new Register8();
              System.out.println(Binary1.getBit(3));

    Hey there liddle buddy,
    How did you define 'bits'
    Thang yer very much yer really wonderful
    *** ELVIS: He's the MAN ***
    This answer was supplied coutesy of : -
    Elvis Presley: Programming Consultants inc.
    contact: [email protected]
    Thang yer very much

  • What are some best practices to work with Compressor 3 droplets and new Compressor 4?

    I have purchased Final Cut Pro X / Motion 5 and Compressor 4 from the App Store. My installation of Final Cut Studio 6.0.6 / Motion 3.0.2 / Compressor 3.0.5 has been moved to a new folder "Final Cut Studio". I have started the old version of Motion first as advised in this document.
    I just tried to use my old Compressor 3 droplets and i am able to convert as before.
    I am wondering what I should do now and what the best practices are
    Use the old droplets and stick with Compressor 3?
    Import the droplets to a new location to be able to use them in Compressor 4?
    Recreate new droplets with similar settings within Compressor 4?
    What are the changes I can expect from using Compressor 4?

    I've found that it helps to meet with each person in your new org that you'll be supporting. Start with the Eloqua Admin(s) and ask for an overview of standard reports, naming conventions, folder structure, processes, etc. Make sure to do the same with sales operations, too. If you're moving into an admin role ask for the data dictionary so you can study it to understand how the data maps from front end CMS to MAP to CRM.
    The next phase would be to meet with field marketing and campaign managers. Ask them about current projects, future projects and how you'll support their success with Eloqua.
    And last but certainly not least - process documentation! Ask for all of those things and you'll be rocking and rolling in no time.
    Good luck in your new role!

  • I have some loops from acid 5 which I have dragged in a folder to the loop browser.  Some of them retain the folder name in the View: and some don't.  I have had some that seem to go in but I can't find them.  Where should I look?

    I have some loops from acid 5 which I have dragged in a folder to the loop browser.  Some of them retain the folder name in the View: and some don't.  I have also had some that seem to go in but I can't find them.  If I try to add them again, I get a msg that they are already there.  Where should I look?

    It may be best to recreate the folder and the smart playlists from scratch.
    tt2

  • Some loops not showing.

    i installed gb3 on top of gb1, i found almost every loop appears twice in the loop browser. i then trashed the old "apple loops for garageband" folder and the old "apple loops index" folder.
    after that, problem solved, but some loops are now missing in the loop browser. i've checked, the "missing" loops are still in the loop folder.
    why would that happen?
    how to solve?
    k.

    Make sure Keyword BRowsing is turned off in GB's prefs. Also any loops not in the same time signature as your song won't be displayed.

  • I was transferring my information from my old iphone to the new one, and i forgot that i had some pictures in the new one. i did the restoring information and the pictures in new one are gone. how can i get them back????

    i was transferring my information from my old iphone to the new one, and i forgot that i had some pictures in the new one. i did the restoring information and the pictures in new one are gone. how can i get them back???? please

    Only way to get it back is to restore your phone from a backup, if those pics were included in that backup.
    But most likely those pics are gone.
    What you should've done was import http://support.apple.com/kb/HT4083 the pics to your computer and after you restored from your old backup, you could sync those new pics to your phone via itunes.

  • Help, please! Any advice on how I link my new iMac and my new itunes software with existing external harddrive containing my entire itunes library?

    Help, please! Any advice on how I link my new iMac and my new itunes software with existing external harddrive containing my entire itunes library?

    Hello!
    It should be a matter of
    Quiting iTunes first if it's running
    Connecting your external harddrive to your Mac, use the Finder to browse to it
    Find your iTunes folder on the ext HDD and double-click on iTunes Library.itl
    If you have any trouble with this, please let us know.

  • Error message when exporting: Some export undefined global: new Grain Seed (1) [was:help]

    Trying to export my pics to a folder on my desk top This message pops up Some export undefined global: new Grain Seed (1)
    Does anyone know what this means?
    Thanks, Donna

    Hi,
    This has occured previously, have a look at http://forums.adobe.com/message/6283140#6283140
    I would also make sure that a valid option is selected in the 'Post Processing' part of the export panel.

  • Im having trouble sending messages from my iPhone 4..ever since i upgraded to iOS 5, its giving me this problem. It says your SMS mailbox is full. Delete some messages to recieve new messages.  I've deleted around 10 huge conversations, rebooted it, resto

    Im having trouble sending messages from my iPhone 4..ever since i upgraded to iOS 5, its giving me this problem. It says your SMS mailbox is full. Delete some messages to recieve new messages. 
    I've deleted around 10 huge conversations, rebooted it, restored it, done a soft and hard reset too. What should i do?

    my boyfriend restored his iphone as a brand new phone and still had this problem, he deleted all his text conversations except mine and his best friend's conversations still nothing, i told him to delete OUR conversation since its a 2 year conversation that he has NEVER deleted ..... he said YES!! we tried to delete it and the phone freezes as soon as we click on the CLEAR CONVERSATION button!!!! we tried about 5 times and this happened everytime we tried!!!! ....is like the phone doesnt wanna let go of our conversation!!! every other conversation he deleted the phone was fine except for when we try and delete ours!!
    so yesterday he left his phone alone around 4 pm since we have the apple appointment today at 130pm.......around 11 pm last night his phone went NUTS receiving all the text messages i had sent and never received!!!! and now the phone works just fine!!! ...... CONFUSED?????? yeah we are too!!!
    but we're still going to the apple store to see what they can tell us!!!

  • DVD problem with missing loops, instruments

    (trying to get a helpful subject there for those searching for answers)
    the fact that the loops are greyed out and instruments missing in new garagebands is well known. so is the solution of installing from DVD. i cant seem to get that to work for my high school music class.
    back story is that every student has a macbook (yes i know.. amazing). thing is, most students give their macs to the IT department to get their machines 'set up' and the IT department collects their DVDs. i borrowed a bunch of those DVDs from the IT department and inserted the DVD when asked but its just not working..
    any ideas? do i need to get the EXACT DVDs that came with the machine? i doubt that.
    also - why doesnt any of the download options work? does anyone from apple know thats broken?
    lastly - what about instruments? i can get the students some loops by dragging loops of my machine into garageband (using target disk mode), but how do i get instruments? i dont drag them into the loop browser, so where might they get put? should i put them in /Application Support/?
    as we're talking about 37 students (this semster - last semester we had an ugly fix) i thought i'd ask before i did something.
    thanks a bunch..

    http://www.bulletsandbones.com/GB/GBFAQ.html#missingloops

  • I have copied my iTunes library onto a new computer, but when I play back the songs there seems to be random "miss" or "drop out".  No probs with old computer, just the new one.  I'm onto my third new computer - same problem.  Any suggestions please?

    I have copied my iTunes library onto a new computer, but when I play back the songs using the new computer, there seems to be random "miss" or "drop out" during play back, similar to a small scratch on a vinyl record.  Although when I play the same song over again, the "miss" occurs in a different location during the song.  No probs with old computer, just the new one.  I'm onto my third new computer - same problem.  Any suggestions please?

    You don't say which version of iTunes you are using, but the symptoms you describe have been encountered by some users with the 64-bit version of iTunes 12.1.
    There is an alternative version for 64-bit editions of Windows here:
    iTunes 12.1 for Windows (64-bit — for older video cards)
    that seems to address the most of the playback issues that some users have been reporting with the full 64-bit version of 12.1.0.77 - including problems with Bluetooth speakers where either sound is degraded or completely absent.  The alternative installer here is the same architecture as most previous "64-bit" versions of iTunes, i.e., a 32-bit application with a 64-bit installer.  The "for older video cards" label appears to be something of a misnomer as some people have reported that this corrects problems found on current / high-spec systems.
    Some users have reported that after installing this alternative version it is necessary to adjust the "Play audio using" selection (normally to "Direct Sound" rather than "Windows Audio Session", though this may vary based on your audio hardware/drivers) in Edit > Preferences > Playback to restore glitch-free audio (one part of the reported symptoms is that the "Direct Sound" choice isn't available, usually in Windows 8).  It may also be necessary to ensure that your Bluetooth speakers are active and selected as the default audio device before you run iTunes.
    If this doesn't address the issue, you may want to think about reverting to iTunes 12.0 - see Turingtest2's notes on Troubleshooting issues with iTunes for Windows updates for steps needed to completely remove all installed iTunes components, and for links to the 12.0 installers.

Maybe you are looking for

  • ITunes can't see my iPOD Music

    iTUnes can'r see mhy Fifth Gen iPOD Music Posted: Mar 25, 2007 1:12 AM Click to reply to this topic Reply email Email I keep my laptop music on an external drive. Today the drive accidentally got disconnected and thus iTunes (latest version Win XP) d

  • Payment Type Your payment method is not valid in this Store. Please enter a new payment method.

    Hi All, I am suffering for long time with subjected error BUT UNFORTUNATELY iTune doesn't have any salutation for this which is strange, same Payment Method is working for old devices BUT NOT WORKING with new bought "Mac Book Pro" and now don't know

  • ORA-02019 while using DBMS_FILE_TRANSFER Package.

    Hi, I am trying to transfer the datafiles from 10.2.0.3 database residing on File-system to 11gR2 database residing on ASM. Both the DBs are on Different machines across datacenters. I am trying to use Transportable Tablespace to move the data. As a

  • Macbook pro Power adapter disconnects

    After charging the macbook and then disconnecting the power adapter, but I still leave the adpater plugged to the wall, it appears it just stops working or it shuts itself off. When I plug back in again after several hours there is no juice to the la

  • Repeated free call alert on my Lumia 520.

    I want to free call alert by repeted beep in my nokia Lumia 520 Moderator's Note: Post was moved and changed the title into a subject-related title.