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: Program of an Array in Java

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program of an Array in Java

    import java.io.*;
     
    public class ArayKo{
      public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("How many elements you want to enter in the array: ");
        int num=0;
     
        try{
          num = Integer.parseInt(in.readLine());
        }
        catch(NumberFormatException ne){
          System.out.println(ne.getMessage() + " is not a number!");
          System.exit(0);
        }

    Hi, im new to this language and need help how to produce this output

    Enter number of Arrays:
    Num[0]:__
    Num[1]:__

    Where number of arrays will depend on user input on screen and will print it in ascending, descending order and will show the highest and lowest input in the array! Please lighten up...thanks !


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Array

    Hello eric and welcome to the Java Programming Forums.

    You can use this code below. I have used the Scanner class to take user input from the console.
    This will then set the size of the array depending on what int value the user enters:

    import java.util.Scanner;
     
    public class Eric {
     
        public static void main(String[] args) {
     
              System.out.println("Enter Array size: ");
              Scanner sc = new Scanner(System.in);
     
              int arraySize = sc.nextInt();
     
              String[] myArray = new String[arraySize];
     
        }
     
    }
    To take this one step further, ive added this code so the user can now populate each array field and once its filled it will print its results:

    import java.util.Scanner;
     
    public class Eric {
     
        public static void main(String[] args) {
     
              System.out.println("Enter Array size: ");
              Scanner sc = new Scanner(System.in);
     
              int arraySize = sc.nextInt();
     
              String[] myArray = new String[arraySize];
     
              System.out.println("Array size set to " + arraySize);
     
              //Populate the array
              for(int a = 0; a < myArray.length; a++){
                  System.out.println("Enter Array value [" + a + "]:");
                  myArray[a] = sc.next();
              }
     
              //Print array values. Can also add Array sort code here.
              for(int b = 0; b < myArray.length; b++){
                  System.out.println(myArray[b]);
              }
     
        }
     
    }
    To sort the Array you could use the java.util.Arrays class:

    http://www.javaprogrammingforums.com...ays-class.html

    Let me know if you get stuck..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Tags for this Thread