Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
[Tutorial] Arrays
Author Message
cool dude

Posted: 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){         }     } }

Krabjuice

Posted: 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

Posted: 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

Posted: 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

Posted: 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

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

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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: