0

I want a bit of help regarding the code I have written just as a sample. And I am currently using Atom Notepad for my codes.

Problem Statement:

Take user input for Plane Details.

Expected Output:

Print the user entered data.

Problem:

I am able to successfully take input from the user but while printing, all the values are shown as NULL.

CODE:

import java.util.*;
class Planes{
  String Name;
  int Number_of_Pylons;
  String Type;

  void EnterValues()
    {
      Scanner s=new Scanner(System.in);
      System.out.println("NAME: ");
      String Name=s.nextLine();
      System.out.println("PYLONS: ");
      int Number_of_Pylons=s.nextInt();
      s.nextLine();
      System.out.print("TYPE OF CRAFT: ");
      String Type=s.nextLine();
    }


};
class Base{
  public static void main(String args[]){
    Planes air[]=new Planes[2];//DID NOT CREATE OBJECTS, REFERENCE ARRAY!!!!!!!!!
    for(int j=0;j<2;j++){
      air[j]=new Planes();
    }// CREATED OBJECTS OF CLASS PLANE!!!!!!!!!
    int i=0;
    for(i=0;i<2;i++)
    {
      System.out.println("Enter values for the planes:");
      air[i].EnterValues();
    }
    System.out.println(air[0].Name);//Test for Print, prints NULL idk why :(
      for(i=0;i<2;i++)//Display the Entered Details
      {
      System.out.println("Name: "+air[i].Name+"Number of Pylons: "+air[i].Number_of_Pylons+"Type: "+air[i].Type);
      }
    }
  };

It is a fairly simple code but i cant seem to be able to get it to work. Also I apologise for my English. Its not my first language.

Community
  • 1
  • 1
NewJava
  • 7
  • 4
  • I "think" the reason for your issue is: https://stackoverflow.com/q/13102045/592355 (unexpected values, when using `nextInt()` in combination with `nextLine()`) – xerx593 Mar 18 '20 at 09:12
  • solution: OR(1.) get your int via: `int Number_of_Pylons=Integer.valueOf(s.nextLine()); //possible runtime exception!` ...OR(2.) get (all of) your "strings" via `s.next()` (Problem with 2. - it reads only until "blank") – xerx593 Mar 18 '20 at 09:15
  • 1
    but `s.next` will just take the first token value and not the whole sentence right? – NewJava Mar 18 '20 at 09:22
  • option 3 (as described in referenced & accepted answers): Invoke `nextLine()` after `nextInt()` ;) – xerx593 Mar 18 '20 at 09:25

2 Answers2

1

Its a classic scope problem. I think you have not learnt the scope-concept well in Java.

class Planes{
  String Name;  //instance variable
  int Number_of_Pylons;//instance variable
  String Type;//instance variable

  void EnterValues()
    {
      Scanner s=new Scanner(System.in);
      System.out.println("NAME: ");
      String Name=s.nextLine(); //local variable
      System.out.println("PYLONS: ");
      int Number_of_Pylons=s.nextInt(); //local variable
      s.nextLine();
      System.out.print("TYPE OF CRAFT: ");
      String Type=s.nextLine(); //local variable
    }


};

I have commented on the above code about type of variables you have used. So, if you write something like

String Name=s.nextLine(); 

It just assigns the value to your local variable Name whose scope is inside the function alone. This does not assign anything to your object. If you want to assign values to object properties it must be done like

this.Name=s.nextLine();

When you specify this this the scope is instance level. Now your object property Name has the value. Similarly for others too. You can fix remaining and make it work.

BTW, the naming conventions used, formatting of code etc., feels like a .Net model. Get familiarized with best practices in java coding - because its all broken here!.

Kris
  • 5,560
  • 3
  • 31
  • 55
  • So, for the local variables to get the user value `this` keyword is important? – NewJava Mar 18 '20 at 09:20
  • You have to assign values to instance variable, if you want to access the values with object. If you assign to local variables, they are not object properties, so object properties will be null – Kris Mar 18 '20 at 09:22
  • Okay, will keep that in mind form now on. Also is Atom Notepad okay for coding Java as i have to use the terminal to run my code – NewJava Mar 18 '20 at 09:24
  • Okay cool! I am a first year student and i am not really familiar with the environments. Thanks – NewJava Mar 18 '20 at 09:27
  • You have all the time in world! All the best – Kris Mar 18 '20 at 09:29
0

You did ask the user for values, but you did not set these values to the current object

void EnterValues(){
    Scanner s = new Scanner(System.in);

    System.out.println("NAME: ");
    this.name = s.nextLine(); // assign the value to the object's name

    System.out.println("PYLONS: ");
    this.number_of_Pylons=s.nextInt();
    s.nextLine();

    System.out.print("TYPE OF CRAFT: ");
    this.type=s.nextLine();
}

Also, Java convention is lowerCamelCase for variable names

azro
  • 35,213
  • 7
  • 25
  • 55