简体   繁体   中英

Java. User's input is working in main class through instance variable but not in other class

Java. User's input is working in main class through instance variable but not in other class

Users input value of song1 variable of class song in working in songtestdrive main class but not in the recording class. In Recording class it prints the user's value as NULL

import java.util.Scanner;

class songtestdrive{ 
public static void main(String[] args)
{   song rock = new song(); 
    recording record = new recording();
    rock.songtype();
    record.recording();
    // here song class's user value is working with rock.song1 
    System.out.println("So you are "+rock.song1+" Music lover");
}}

class song
{   Scanner in = new Scanner(System.in);

String song1;
 String songtype()
{   System.out.println("Which type of songs you like");
    song1= in.nextLine();
    return(song1);
}}

class recording
{
String record,yesno;
  public void recording()
{   song song_recording = new song();
    // need help here song_recording.song1 is unable to show the user's input instead showing null
    System.out.println("Do you want to record songtype "+ song_recording.song1);
}}

The song ( rock ) instance you create in main and the song instance you create in recording ( song_recording ) are different.

You can pass the rock variable to the recording method

public void recording(Song song)
{  
    //song song_recording = new song(); <-- Remove this
    System.out.println("Do you want to record songtype "+ song.song1);
}

The caller becomes

public static void main(String[] args)
{   song rock = new song(); 
    recording record = new recording();
    rock.songtype();
    record.recording(rock);
    System.out.println("So you are "+rock.song1+" Music lover");
}

Or the recording class constructor if a recording instance is always related to one song.

class recording {
    String record,yesno;
    Song song;
    class recording(Song song) {
        this.song = song;
    }
     public void recording() {
         //song song_recording = new song(); <-- Remove this
        System.out.println("Do you want to record songtype "+ song.song1);
    }
}

The caller becomes

public static void main(String[] args)
{   song rock = new song(); 
    recording record = new recording(rock);
    rock.songtype();
    record.recording();
    // here song class's user value is working with rock.song1 
    System.out.println("So you are "+rock.song1+" Music lover");
}

Note: Follow Java naming conventions to name class names. Class names should start with an uppercase letter... Song , Recording ...

Variable names must follow camel case style - songRecording

You need to pass the song to the recording class, by adding constructor:

class recording
{
String record,yesno;
song rock;
  public recording(song rock) {
this.rock = rock;
  }
public void recording()
{   
    // need help here song_recording.song1 is unable to show the user's input instead showing null
    System.out.println("Do you want to record songtype "+ rock.song1);
}}

And update your main code:

public static void main(String[] args)
{   song rock = new song(); 
    recording record = new recording(rock);
    rock.songtype();
    record.recording();
    // here song class's user value is working with rock.song1 
    System.out.println("So you are "+rock.song1+" Music lover");
}}

This code snippet gets you the output that you are looking for:

public class songtestdrive{ 
      public static void main(String[] args) throws IOException{         
          song rock = new song(); 
          //recording record = new recording();
          rock.songtype();
         // We take the recording object out of the code because it is a different obj.
          System.out.println("So you are "+rock.song1+" Music lover");
     }}

class song{   
     Scanner in = new Scanner(System.in);

 String song1;
 String songtype(){         
        System.out.println("Which type of songs you like");
        song1= in.nextLine();
        recording record = new recording();
        record.recording(song1);
        return(song1);
 }}

class recording extends song{

     String record, yesno;
     public void recording(String song1){       
     // need help here song_recording.song1 is unable to show the user's input instead showing null

     System.out.println("Do you want to record songtype " + song1 + "?");
 }}

The output is as follows:

Which type of songs you like
rock
Do you want to record songtype rock?
So you are rock Music lover

The issue in your code originally is that you create a song object and a recording object, and then you expect the recording object to receive the variable "song1" from the song object. This is not possible however. How I edited your code was by simply triggering the recording object by passing an argument to it with it is instantiated within the song class. This way it has a reference to the song1 variable. The way that you structured your code, song1 would never be known to the recording object, which is why it would return "null". In the song class we simply create our recording object THERE, and then we pass it the song1 argument so that it is aware of that variable. This gets you the output that you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM