0

Importing libraries

import java.io.*;
import java.util.*;

Making one new Stock class

class stock implements Serializable {

    String name;
    long price;
    String listedOn;




 void set_data(Scanner s1) {
        System.out.println("enter name of the stock:: ");
        name = s1.nextLine();
        System.out.println("Enter price of the stock:: " );
        price = s1.nextLong();



   

Whenever I try to take user input for more than one String variable in this class it just skips that second String user input. In this scenario, I am trying to take String user input and storing it in listedOn variable but it just Skips that line. But if I change that Variable into any other primitive data it works fine it only skips second String user input.

   System.out.println("Enter on which index it's listed on:: ");
   listedOn = s1.nextLine();

If I take user input of any other primitive data type then it will work all fine for Example

//s1.nextInt() //s1.nextLong() //s1.nextFloat()

    }

    void get_data(){
        System.out.println("Stock Name:: "+name);
        System.out.println("Stock price:: "+price);
        System.out.println("Listed On:: "+listedOn);
    }

}

public class q11 {

    public static void main(String[] args) throws IOException {
        Scanner s1 = new Scanner(System.in);
        stock sto1 = new stock();
        File f1 = new File("test_obj1.txt");
        FileOutputStream file_out = null;
        FileInputStream file_in = null;
        ObjectOutputStream obj_out = null;
        ObjectInputStream obj_in = null;
        

        try {

            file_out = new FileOutputStream(f1); //Creating FIle Output Stream 
            obj_out = new ObjectOutputStream(file_out); //passing FileOutputStream object to ObjectOutputStream and assigning it to obj_out object
            
            sto1.set_data(s1); //Calling Stock class function set_data() to take user input 
            obj_out.writeObject(sto1); //Writing class stock object to obj_out object by WriteObject() Function  
            obj_out.flush();
         
            

            file_in=new FileInputStream(f1); //Passing f1 file and assigning it to File_in object
            obj_in=new ObjectInputStream(file_in); //passing FileInputStream object to ObjectInputStream and assigning it to obj_in object

            stock sto2=(stock)obj_in.readObject(); //Reading stock object from obj_in object using readObject() function and assigning it to stock Second Object sto2

            sto2.get_data(); //calling function to read object from file
           

        } catch (Exception e1) {
            System.err.println(e1);
        } finally{
            
            obj_out.close();
            file_out.close();
            file_in.close();
            obj_in.close();
        }

    }

}
  • You should look into what nextLong does and where the notional input pointer is after a nextLong. Then you should consider what nextLine will do starting from that position. – iggy Jul 16 '21 at 01:01

0 Answers0