Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Arrays
Index -> Programming, Java -> Java Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Thu May 11, 2006 3:28 pm   Post subject: (No subject)

this is a sample program i made to show you how. i might be mistaken because i just started java but this is using my knowledge from other languages

code:

import java.io.*;

public class arrays {
    public static void main (String[]args){
        int[] numbers = new int[5];
        String number;
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for(int i = 0; i<5; i++){
            System.out.println("enter 5 numbers");
            number = in.readLine();
            numbers[i] = Integer.parseInt(number);   
            }
            for(int j = 0; j<5; j++){
                System.out.println("The numbers you entered are " + numbers[j]);
            }
        }catch(IOException nfe){
        }
    }
}
Sponsor
Sponsor
Sponsor
sponsor
Krabjuice




PostPosted: Fri May 12, 2006 2:53 pm   Post subject: (No subject)

Well, if you already know how large the array is, a simple for loop is all you need.

Make sure you start your loop at 0, as the index for an array begins at 0--not 1. Thus start=0, end=(size-1)

For instance:

code:

import java.util.Scanner;
public class ArrayInput
{
     public static void main (String[] args)
     {
          Scanner input = new Scanner(System.in);
          int[] get = new int[10];
          for (int i=0;i<=9;i++)
          {
               get[i]=input.nextInt();
          }
     }
}
cool dude




PostPosted: Fri May 12, 2006 3:44 pm   Post subject: (No subject)

Krabjuice wrote:
Well, if you already know how large the array is, a simple for loop is all you need.

Make sure you start your loop at 0, as the index for an array begins at 0--not 1. Thus start=0, end=(size-1)

For instance:

code:

import java.util.Scanner;
public class ArrayInput
{
     public static void main (String[] args)
     {
          Scanner input = new Scanner(System.in);
          int[] get = new int[10];
          for (int i=0;i<=9;i++)
          {
               get[i]=input.nextInt();
          }
     }
}


dude thats pretty much the same thing i wrote! except your using scanner to get input and i'm using bufferedreader.
Krabjuice




PostPosted: Fri May 12, 2006 7:50 pm   Post subject: (No subject)

True. However, not everyone uses the classic buffered reader method. Pick and choose, it would seem the situation is.
cool dude




PostPosted: Fri May 12, 2006 7:55 pm   Post subject: (No subject)

Krabjuice wrote:
True. However, not everyone uses the classic buffered reader method. Pick and choose, it would seem the situation is.


actually i would have chosen the scanner method but unfortunately it seems not to work, even though i downloaded the latest version of java from the javasun website.
Krabjuice




PostPosted: Fri May 12, 2006 8:02 pm   Post subject: (No subject)

Well, now we have two excellent examples of input and array manipulation.
JMG




PostPosted: Mon Feb 25, 2008 10:14 am   Post subject: RE:[Tutorial] Arrays

what are the limit to arrays in java? good tut btw XD
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: