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 8872099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:08:39+00:00 2026-06-14T18:08:39+00:00

my code is to read lines from a .txt and parse those inputs. inputs

  • 0

my code is to read lines from a .txt and parse those inputs.

inputs are in this form <operation> <category> <name> <price> <quantity> <weight> <optional field1> <optional field2>

Electronics have 2 option fields is it fragile (F/NF) and the state it’s shipping to, groceries have 1 optional field perishable (P/NP). here’s a few example of line inputs

insert clothing shirt 20.50 1 1

insert electronics PS3 300 1 5.2 F NM

insert groceries cabbage 2.00 5 1 NP

Im thinking of writing a legalLine() method to process the errors in inputs

class sampleClass{
          public static void isLegalLine(String lineInput){
    Scanner s = new Scanner(lineInput);
    String operation = null;
    String category = null;
    String name = null;
    float price = 0;
    int quantity = 0;
    float weight = 0;
    String opt1 = null;
    String opt2 = null;
    try{
        operation = s.next(); 
        category = s.next();
        name = s.next();
        price = Float.parseFloat(s.next());
        quantity = Integer.parseInt(s.next());
        weight = Float.parseFloat(s.next());
        if (!operation.equalsIgnoreCase("insert")
                && !operation.equalsIgnoreCase("search")
                && !operation.equalsIgnoreCase("delete")
                && !operation.equalsIgnoreCase("update")
                && !operation.equalsIgnoreCase("print")) {
            System.out.println("invalid operation");
        }
        // more validations for variables category, name, price, quantity, weight goes here
        if(category.equalsIgnoreCase("electronics")){
            try{
                opt1 = s.next();
                opt1 = s.next();
            }catch(Exception e){

            }
        }else if(category.equalsIgnoreCase("groceries")){
            opt1 = s.next();
        }
    }catch (Exception e){
        //general catch for now
        e.getMessage();
    }
}


          public static void main(String[] args) 
      {
               FileReader freader = new FileReader(args[0]);
               BufferedReader bfrReader = new BufferedReader(freader);
               isLegalLine(bfrReader.readLine());


                //program does calculation for total price of input
}

you see… I’ve gone through all the trouble setting each token to its respective variables in the isLegalLine(), how can I pass those information back to the main()? and is this a good design for processing input line errors?

  • 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-06-14T18:08:41+00:00Added an answer on June 14, 2026 at 6:08 pm

    Since you will need to process them as well, a good solution I see is this:

    Create a Product class with three subclasses: clothing, electronics, and groceries

    Then make your function

    public static Product isLegalLine(String lineInput)
    

    return the Product after parsing the line.

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

Sidebar

Related Questions

So far, I got my code to read from a .txt file, parse the
Hi I am using the following code to read some values from lines containing
am trying to read few lines from a txt file using JS,and i have
I want to read a specific line from an html source code. Im storing
I need code for read json data from url in ios (objective c) If
How do you read lines from a vector and compare their lengths? I have
I have this code: <script type=text/javascript> var url = http://www.xxxxx.xxx/xxxxxxxxx; var txt; var id1;
How to read line by line and parse from a file in python? I
I have an app in which I parse a .txt file from a URL
I am exporting UTF-8 text from Excel and I want to read and parse

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.