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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:50:49+00:00 2026-05-25T19:50:49+00:00

This should be pretty simple but for some reason, I can’t get this program

  • 0

This should be pretty simple but for some reason, I can’t get this program to take arguments from a .txt file and output it. Here’s my code:

public class Lab2 {

static Scanner input = new Scanner(System.in);

public static void main(String[] args)
{   
    Lab2_CLA.Lab2_CLA(args);        
}
}

public class Lab2_CLA {

public static void Lab2_CLA(String[] data)
{
    System.out.printf("%s, %s: %s%n", data[1], data[0], data[2]);
}
}

I compiled the code and it works if I give it arguments through the terminal. Ex:

Typing “java lab2/Lab2 John Doe 12345678”

Prints out: “Doe, John: 12345678“

However, how can I get it to read data from a .txt file by typing the following into the terminal: “java lab2/Lab2 < input.txt“? Whenever I do this, I get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at lab2.Lab2_CLA.Lab2_CLA(Lab2_CLA.java:12)
at lab2.Lab2.main(Lab2.java:34)
  • 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-25T19:50:50+00:00Added an answer on May 25, 2026 at 7:50 pm

    Input redirection sends the file to the program’s stdin (standard input file), not in its command line parameters. On a Unix-alike you can obtain the effect you want by using the backtick operator:

    java lab2/Lab2 `cat input.txt`
    

    In short, the text between the backticks is executed, and the result of the execution will be inserted in its place.

    EDIT (responding to OP’s edit):

    You get an out of bounds exception because with the redirection the program doesn’t get any command line parameters, yet hard-address elements 0 1 and 2 of the args array.

    Should for some reason the backtick based solution not be possible for you the only option will be to modify your program to either read its stdin (technique show here Java file input as command line argument), or accept a filename as an argument, and read that file.

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

Sidebar

Related Questions

This should be a really really simple thing, but for some reason it is
This should be a simple question, but I haven't been able to find a
This should be relatively simple to do but I've yet to find a description
New Rails programmer here. There is probably a pretty simple solution to this, but
I'm pretty sure there should be a more Pythonic way of doing this -
This should hopefully be a simple one. I would like to add an extension
This should a quick question for some easy rep. I'm doing some PHP Website
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
This should be relatively simple, here is my situation; I have copied the trunk

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.