Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:00+00:00 2026-05-27T05:47:00+00:00

I would load from a file a string and put in in a object

  • 0

I would load from a file a string and put in in a object enum.

I create into a class this:

public Passenger{

private String pass_name;
public enum State{

b,c,d;

};
....

State reser;

public Passenger(String n,State r)
      pass_name=n;
      reser=r;
}

in another file I want read a file and put the string in the way i can create an object passenger such:

Passenger p=new Passenger(p_name,What should i put here)?

here is the structure of my file:

cod
passenger_name
reverved

cod and passenger will be rapresented by String while reserved should be rapresented through enum. I will read the file:

 BufferedReader reader=new BufferedReader(new FileReader(fname));

 String cod=reader.readLine();
 while(cod!=null){
      String p_name=reader.readLine();
      how can i load a enum type?

      Passenger p=new Passenger(p_name,What should i put here)?
      cod=reader.readLine();

 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:47:01+00:00Added an answer on May 27, 2026 at 5:47 am

    Use YourEnumType.valueOf to convert the String you read from the file into the enum:

    public class Passenger {
        public static enum State {
            New,
            Reserved,
            Paid
        }
    
        private String name;
        public State state;
    
        public Passenger(String name, State state){
            this.name = name;
            this.state = state;
        }
    
        public String toString() {
            return String.format("Passenger{name='%s', state=%s}", name, state.name());
        }
    }
    
    class Main {
        public static void main(String[] args) {
            // line read from file
            String stateString = "Reserved";
            // convert string into state
            Passenger.State state = Passenger.State.valueOf(stateString);
    
            // create the passenger
            Passenger pass = new Passenger("John Doe", state);
            System.out.println(pass);
        }
    }
    

    (I took the liberty of making a cleaner version of your sample code.)

    Make sure that the string in the file is the same as the name of your enum constant, including capitals, and that it doesn’t include extra whitespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to load the content of a file into a string. This
Why doesn't this code want to load all the values from a text file?
How would I read from an Excel sheet and load the marked selection (Area)
I would like to load a BMP file, do some operations on it in
$('#selector').click(function() { // here I would like to load a javascript file // let's
I am reading in a String from a text file which contains a date
I need to load some text (shader code) from a file that is placed
Consider the 2 following methods of reading a string from a file: NSString *path
I am trying to load assets from an SWC into a SWF at compile
I've downloaded an HttpHandler class that concatenates JS files into one file and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.