Skip to main content
deleted 21 characters in body
Source Link
Lloyd aaron
  • 292
  • 2
  • 4
  • 10

iI have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there aanother way to do this that I'm missing, or am I justprobably missed? Am I messing something up? If not, how should I go about doingdo this to enable me to get the same functionality as below?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;
    
    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();
    
     System.out.print("Input the Offset : ");
     offset = input.nextInt();
     
    NumberList numberlist= new NumberList(length, offset);
    
    

    
}


private static void question(){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();
    
    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \\ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \\ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \\ error
    }
}

i have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there a way to do this that I'm missing, or am I just messing something up? If not, how should I go about doing this to enable me to get the same functionality as below?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;
    
    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();
    
     System.out.print("Input the Offset : ");
     offset = input.nextInt();
     
    NumberList numberlist= new NumberList(length, offset);
    
    

    
}


private static void question(){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();
    
    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \\ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \\ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \\ error
    }
}

I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;
    
    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();
    
     System.out.print("Input the Offset : ");
     offset = input.nextInt();
     
    NumberList numberlist= new NumberList(length, offset);
    
    

    
}


private static void question(){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();
    
    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \\ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \\ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \\ error
    }
}
edited tags
Link
Lloyd aaron
  • 292
  • 2
  • 4
  • 10
Source Link
Lloyd aaron
  • 292
  • 2
  • 4
  • 10

How can i access an object from another method in java?

i have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there a way to do this that I'm missing, or am I just messing something up? If not, how should I go about doing this to enable me to get the same functionality as below?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;
    
    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();
    
     System.out.print("Input the Offset : ");
     offset = input.nextInt();
     
    NumberList numberlist= new NumberList(length, offset);
    
    

    
}


private static void question(){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();
    
    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \\ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \\ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \\ error
    }
}