Results 1 to 4 of 4
  1. #1
    Fridtjof is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default struggling with arrays

    Hei!

    Im struggling like a pig to create this:

    Create a program that contains the array
    int [] a = {1, 4, 5, -2, -4, 6, 10, 3, -2}
    a) Count the number of negative numbers are in the array using a for-loop and
    enter the result in the terminal
    b) Count the number of negative numbers are in the array using a while-loop
    and enter the result in the terminal
    c) Replace all negative numbers with their position in the array (where positions are counted
    from 0 and up).
    _____

    I didnt get so far...
    _____________________________
    import java.util.Scanner;

    public class arrays4 {
    public static void main(Strung[] args) {


    int[] a = {1,4,5,-2,-4,6,10,3,-2};

    for (in i =0; i <9; i +=1) {

    if (

    }
    }
    }
    __________________
    Any help would be greatly appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: struggling with arrays

    First, change Strung to String in main.

    Do you know how loop over an array?
    That is the first step in completing the assignment.
    Try doing that and printing out the each value
    in the array.

    For more help, check out. Arrays (The Java Tutorials > Learning the Java Language > Language Basics)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Fridtjof is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: struggling with arrays

    Thx. The link doesnt helped, i'm too dumb to figure out how to continue.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: struggling with arrays

    I presume that you are taking a class in Java. Have you gone over this in class? If you didn't understand something
    did you ask the teacher questions? This is so basic that you need to talk to your instructor asap for help. The
    purpose of this forum is to assist folks in debugging their completed code. Here is a hint to get you started.

    Java Code:
    int [] a = new int[3]; // an array of length 3.
    
    a[0] = 10; // assign each position in the array a value.
    a[1] = 2;
    a[2] = 4;
    You can index an array with a variable.

    Java Code:
    int n = 1;
    System.out.println(a[n]); // prints 2.
    Now try using a for loop to print all three values. And please discuss with your teacher.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. struggling with properties
    By willemjav in forum Advanced Java
    Replies: 4
    Last Post: 11-17-2013, 01:56 PM
  2. Struggling need help
    By acbar in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 06:00 AM
  3. Struggling to import JAR
    By Mr ChriZ in forum Android
    Replies: 0
    Last Post: 01-12-2012, 11:53 AM
  4. Struggling with this code
    By Shimless12 in forum New To Java
    Replies: 5
    Last Post: 07-14-2011, 07:06 PM
  5. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 06:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •