help with input from arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: help with input from arrays

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    help with input from arrays

    Down below I have X and Y. These are Going to be like coordinates on a grid. At the INput screen the User is going to input java elephant number secondnumber, I would like these number and secondnumber to add to the x and y coordinates given. I have a for loop but this isn't working can someone help that the user input with increase the X and Y Coordinates.

    import java.awt.*;
    import java.applet.*;
    import java.awt.Color;

    public class elephant extends animal {
    private static int x = 20;
    private static int y = 20;
    private static Color c = Color.blue;

    public static void main(String [] args) {

    int coordinate;
    int coordinate2;
    int position;

    elephant elephant = new elephant(x, y, c);

    // I set up these for loops to attempt to add on to the x
    // and y values but it's not working, Anyone help?

    for (int k = 0; k < args.length; k++) {
    for (int i = 1; i < args.length; i ++) {
    if (args.length > 0) {
    coordinate = Integer.parseInt(args[k]);
    coordinate2 = Integer.parseInt(args[i]);
    x = coordinate;
    y = coordinate2;

    }
    }
    }
    }
    public elephant (int xx,int yy, Color cc){
    x = xx;
    y = yy;
    c = cc;
    }
    public void paint(Graphics g) {
    drawhead(g,200 + x,150 + y,80,80);
    drawneck(g,280 + x,165 + y,10,50);
    drawbody(g,290 + x,165 + y,150,150,10,10);
    drawoutleg(g,290 + x,315 + y,20,70);
    drawoutleg(g,310 + x,315 + y,10,50);
    drawoutleg(g,420 + x,315 + y,20,70);
    drawoutleg(g,410 + x,315 + y,10,50);
    drawtail(g,440+ x,165+ y,50,10,45,10);
    drawsmile(g,170+ x,200+ y,150,140,100,20);
    draweyes(g,210+ x,160+ y,20,20);
    drawSpots(g,Color.BLUE);
    }
    public void drawSpots(Graphics g, Color c) {
    int[] x = getxspot();
    int[] y = getyspot();
    int [] size = getsize();
    g.setColor(c);
    for (int k = 0; k < 5; k ++) {
    g.fillOval(x[k],y[k],size[k],size[k]);
    }
    }
    public int [] getxspot() {
    int [] array = new int [4];
    array [0] = 300;
    array [1] = 325;
    array [2] = 350;
    array [3] = 365;
    return array;
    }
    public int [] getyspot() {
    int [] array2 = new int [4];
    array2 [0] = 160;
    array2 [1] = 160;
    array2 [2] = 160;
    array2 [3] = 220;
    return array2;
    }
    public int [] getsize() {
    int [] array3 = new int [4];
    array3 [0] = 25;
    array3 [1] = 15;
    array3 [2] = 25;
    array3 [3] = 15;
    return array3;
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    i dont exactly understand why you need a for loop any ways, would this work instead:

    Code:
    if (args.length > 0) {
      coordinate = Integer.parseInt(args[0]);
      coordinate2 = Integer.parseInt(args[1]);
      x = coordinate;
      y = coordinate2;
    }
    ??
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    learn how to use CODE tags: http://forums.javaboutique.internet....bbcode#buttons

    and i'll help you.. right now you code is just a mess, slammed against the edge of the post box

    oh, and in java, we start class names with capital latters.. so your :

    elephant elephant = new elephant()

    is very ahrd for an experienced programmer to read. write this instead:

    Elephant el = new Elephant()

    avoid making the variable the same name as the class
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center