Results 1 to 4 of 4
  1. #1
    niputen12 is offline Member
    Join Date
    Aug 2015
    Posts
    1
    Rep Power
    0

    Default how can i access the array from my first if ?

    i tried many different things .. i tried this code with different class but i failed .. so i tried this with using simple else if statement in just one class but i dont know how can i access my array everywhere


    import java.io.*;

    public class trymenu
    {

    public static void main(String[] args)
    {

    try
    {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);

    rec[] arrStud=new rec[10];



    System.out.println("WELCOME TO MY MENU");
    System.out.println("Enter number 1-4");
    int a = Integer.parseInt(br.readLine());

    if(a==1)
    {
    String ch = " ";

    do
    {

    arrStud[rec.a]=new rec();
    //arrStud[rec.a]=new rec();

    System.out.println("YOU CHOOSE ADD STUDENT");
    System.out.println("Enter Student Name: ");
    arrStud[rec.a].setName(br.readLine());
    System.out.println("Enter Student ID: ");
    arrStud[rec.a].setId(Integer.parseInt(br.readLine()));
    System.out.print("ENTER MATH GRADE: ");



    }
    if(a==2)
    {
    //arrStud[rec.a]=new rec();
    int x =0;

    System.out.println("YOU CHOOSE SEARCH STUDENT");
    System.out.println("ENTER STUDENT ID TO SEARCH: ");
    int ch2=Integer.parseInt(br.readLine());

    for ( x = 0;x<rec.a;x++)
    {
    int csr=arrStud[x].getId();
    if(ch2==csr)
    {
    System.out.println("FOUND!");
    }
    else
    {
    System.out.println("Not Found");
    }
    }
    }
    }
    catch(Exception ff){}
    }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how can i access the array from my first if ?

    So to put it in English: option 1 ("the first if") is to add a student, and what you don't know how to do yet is to add a new student to an empty spot in the array you named 'arrStud'. Correct?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how can i access the array from my first if ?

    how can i access my array everywhere
    Move the array's definition outside of any methods to the class level. Then any method will be able to access it.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: how can i access the array from my first if ?

    The OP has far more problems than that.


    1. Incorrectly nested braces
    2. Missing while construct for the do
    3. Missing rec class
    4. declaring a variable inside a try block (may cause problems later).
    5. Didn't use code tags (see BB Code List - Java Programming Forum - Learn Java Programming)


    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. Replies: 3
    Last Post: 05-02-2013, 09:44 AM
  2. access array from another classes in java
    By flyhigh in forum New To Java
    Replies: 6
    Last Post: 01-22-2013, 06:21 AM
  3. access array list item from draw
    By trishtren in forum New To Java
    Replies: 8
    Last Post: 04-19-2011, 12:34 PM
  4. Access an array from a different class
    By chief100 in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 AM
  5. Replies: 2
    Last Post: 04-30-2010, 05:13 PM

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
  •