Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating an object with a String from file IO

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an object with a String from file IO

    Hi folks,

    I have a chemicals class with various instance variables such as chemical name, boiling point, freeze point etc.

    I have created a stream input file with a .csv extension where I want the user to specify a process stream (flow rate, temperature, chemicals inside the stream etc.).

    I have the following code in the main method:

    Chemicals [] chemObject = new Chemicals[2]; //2 chemicals per stream
    //If i add this line next: System.out.println(inputStream.next()); it prints the name of the chemical I want
    chemObject[0] = new Chemicals(inputStream.next());
    System.out.println(chemObject[0].getName()); //this prints out null meaning that the chemObject[0] did not get created?

    I realized I should have provided more info on the actual Chemicals class and its constructors:

     public Chemicals(String name) //Overloaded consutrctor
      {
        if (name  == "toluene")
        {
          this.name = "toluene";
          this.boilingPoint=110.6;
        }
      }
    public String getName()
      {
        return this.name; 
      }

    How do I create an object with a string from an input stream?

    Cheers


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an object with a String from file IO

    How do I create an object with a string from an input stream?
    String aStr = <get from input stream>; //  read a String from an Input Stream
    SomeClass scObject = new SomeClass(aStr): // create object with String from IS

    You should have a default name value.
    Use the equals() method to compare Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an object with a String from file IO

    thanks Norm.

    It was indeed a problem with the Strings being compared and not with my input streams.

Similar Threads

  1. i/o from jsp to a file (via creating an object)
    By Panoksa in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 26th, 2012, 02:19 PM
  2. [SOLVED] Need some help with creating a new object
    By bankston13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 28th, 2012, 04:31 PM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Using a string when creating an object.
    By ZeroLRS in forum Object Oriented Programming
    Replies: 10
    Last Post: July 13th, 2011, 09:22 PM