Home > OS >  why is my java program printing "null" in output for a string when I took it as an input t
why is my java program printing "null" in output for a string when I took it as an input t

Time:03-24

package com.company;

import java.util.Scanner;

class fields{
    Scanner sc = new Scanner(System.in);
    String fn;
    String ln;
    String em;
    String phn;
    String country;
    String city;
    public fields() {
        System.out.println("\n                    ~~~ FORM ~~~\n");
        System.out.print("First Name: ");
        input(fn);
        System.out.print("Last Name: ");
        input(ln);
        System.out.print("Email: ");
        input(em);
        System.out.print("Phone number: ");
        input(phn);
        System.out.print("Country: ");
        input(country);
        System.out.print("City: ");

        input(city);
    }
    public void input(String x){
        x = sc.nextLine();
    }
    public void output(){
        System.out.println("The user's name is: "   fn   " "   ln   "\nEmail: "   em   "\nPhone Number: "   phn   ",\nand lives in: "   city   ", "   country);
    }
}

public class Form {
    public static void main(String[] args) {

        fields obj = new fields();
        System.out.println("\n                    ~~~ OUTPUT ~~~\n");
        obj.output();

    }
}

I am new to java. and I am still learning its concepts. I think it might be a problem of scopes of variable initialization. I am looking forward to being guided by anyone who can solve my problem. Thankyou

CodePudding user response:

The null values are coming from input method which dosen't initialize values, one of the correct form will be like below

public class Fields {
Scanner sc = new Scanner(System.in);
private String fn;
private String ln;
private String em;
private String phn;
private String country;
private String city;

public Fields() {
    System.out.println("\n                    ~~~ FORM ~~~\n");
    System.out.print("First Name: ");
    this.fn = sc.nextLine();
    System.out.print("Last Name: ");
    this.ln = sc.nextLine();
    System.out.print("Email: ");
    this.em = sc.nextLine();
    System.out.print("Phone number: ");
    this.phn = sc.nextLine();
    System.out.print("Country: ");
    this.country = sc.nextLine();
    System.out.print("City: ");
    this.city = sc.nextLine();
}

public void output() {
    System.out.println("The user's name is: "   fn   " "  
            ln   "\nEmail: "   em   "\nPhone Number: "   phn   ",\nand lives in: "   city   ", "   country);
}}

More over please respect language [conventions][1] for class naming .

[1]: https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html#:~:text=Class names should be nouns,such as URL or HTML).

CodePudding user response:

The problem is on the input() method. This method receives a String parameter. But remember that parameters in java are passed by value and in particular Strings are immutable objects so when you invoke input() and pass a String variable as a parameter, this variable is copied. So the value you are modifying inside the input() method is the copy of the parameter sent. That's why the fn, ln, em, phn, country, city variables are never initialized.

An alternative implementation of input method is the following:

public String input(){
    return sc.nextLine();
}

And invoke it as the following example:

:
System.out.print("Last Name: ");
ln = input();
:

This is just a playful example to understand how parameters are passed in java because using a method just to execute the read from de standard input seems to be unnecessary.

A final comment: As a convention, Class names in java start with a capital letter.

  • Related