entering data into Arrays via Scanner


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: entering data into Arrays via Scanner

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    12

    entering data into Arrays via Scanner

    hey guys, so i've got an assignment for a class where we are to write a menu-driven program to input a directed graph from the keyboard in command line. the program must be designed in a modular fashing having functions carry out a number of tasks.

    basically, i'll have a prompt for the user to enter certain information about a graph (number of vertices, number of edges, then the 1st edge, 2nd edge, ... , last edge based on number of vertices).

    the assignment is meant to practice creating static functions and working with arrays. we will have functions computing adjacency matrix's, path matrix's, degree matrix's... and a few others.

    i'm confused as to how i can have the user input info into a variable and use that to create an array based on the inputted info. so far i'm setting it up like this based on some hints the teacher gave out, can anyone help point me in the right direction and maybe explain what's up with the arrays? also, the "menu-driven" aspect is confusing..

    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Graph{
       public static void main(String[] args) throws IOException{
          //All important variables are defined in the main()
          int[][] adjacency = null; //adjacency mayrix
          int[][] path = null;      //path matrix
          int[][] degree = null;    //in-out degree matrix
    
          int V, E;                 //number of vertices and edges
    
          Scanner keyBoard = new Scanner(System.in);   //keyboard scanner
          Scanner scan = null;                         //file scanner
    
          //read number of vertices here then      
          //allocate memory for the adjacency matrix, path matrix and degree matrix
          //...
       }
    
    
       //Here is a simple function that displays a square array.
       //Note: I have to send the square array variables defined in the main a parameter
       //to this function.
       
       public static void displayMatrix(int[][] m){
       //note: a call to this function looks like:
       //displayMatrix(adjacency); or
       //displayMatrix(path);
       
          for(int r=0; r<m.length; r++){
             for(int c=0; c<m.length; c++){
                   System.out.printf("%2d",m[r][c]);
             }
             System.out.println();
          }
       }
    
    }
    if needed, i can post the actual text of the assignment if this will help explain what i mean.. Thank you very much, you guys have always been extremely helpful to me here..

  2. #2
    Join Date
    Apr 2005
    Posts
    12
    could i just do like a

    V = keyBoard.nextInt();
    adjacency = new int[V];

    ? this doesn't seem right, almost too simple...

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by aesoprock00
    could i just do like a

    V = keyBoard.nextInt();
    adjacency = new int[V];

    ? this doesn't seem right, almost too simple...
    Yep.
    Code:
    
    System.out.print("Enter a size for array: ");
    int[] array = new int[keyBoard.nextInt()];
    
    for (int i = 0; i < array.length; i++) {
        System.out.print("Enter a value for array[" + i + "]: ");
        vertices[i] = keyBoard.nextInt();
    }

Similar Threads

  1. Replies: 8
    Last Post: 10-18-2005, 08:36 PM
  2. Reading data from barcode scanner
    By derek in forum VB Classic
    Replies: 5
    Last Post: 09-21-2005, 11:42 AM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 11:53 AM
  4. Data Source & Data Members
    By Bill in forum VB Classic
    Replies: 0
    Last Post: 09-22-2000, 02:07 AM
  5. Complex Data Consumer Data Aware Classes
    By Desmond Cassidy in forum authorevents.kurata
    Replies: 3
    Last Post: 04-19-2000, 04:54 AM

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