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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:01:33+00:00 2026-06-02T11:01:33+00:00

I am trying to reprogram a Java program that I made into Ruby, to

  • 0

I am trying to reprogram a Java program that I made into Ruby, to help me learn the language. However, I’m having a lot of trouble finding a way to code this particular section of Java code in Ruby:

/* read the data from the input file and store the weights in the 
array list */
Scanner readFile = new Scanner(new FileReader(inFileName));
ArrayList<Weight> listOfWeights = new ArrayList<Weight>();
while (readFile.hasNext()) {
    int pounds = readFile.nextInt();
    int ounces = readFile.nextInt();
    Weight thisWeight = new Weight(pounds, ounces);
    listOfWeights.add(thisWeight);
}

This code takes a file that has a list of integers in two columns (the first being pounds and the second being ounces) like this:

120  2
195 15
200  5
112 11
252  0
140  9

, and makes a bunch of Weight objects using the numbers in each row. Then it adds them to a list. Is there an easy way to do this in Ruby? Here’s what my Ruby program looks like so far:

begin
  puts "Enter the name of the input file > "
  in_file_name = gets
  puts \n

  list_of_weights = []
  File.open(in_file_name, "r") do |infile|
    while (line = infile.gets)

Thanks for the help!

  • 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-02T11:01:40+00:00Added an answer on June 2, 2026 at 11:01 am

    Not equivalent as you asked but since ruby is a dynamic language I think there is no need for such think. So here is how you could do it

      while (line = infile.gets)
        pounds, ounces = line.split(' ')
        p "-#{pounds}- -#{ounces}-"
      end
    

    output

    -120- -2-
    -195- -15-
    -200- -5-
    -112- -11-
    -252- -0-
    -140- -9-
    

    Or a more ruby way (I think)

    File.open(in_file_name, "r").each_line do |line|
      pounds, ounces = line.split(' ')
      p "-#{pounds}- -#{ounces}-"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write this small program to help me in my Stats class, everything
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to add simplecov to my Rails app (Jruby 1.6.4, Ruby 1.9.2, Windows). I
Trying to write a windows speech recognition macro. Written using XML and scripting language
Trying to read in some information from the google maps api into my application
Trying to create a simple document that gets the parentNode then applies a background
I'm trying to get Lua to work with the new programming language D. Everything
I'm trying to reduce the warnings that are sent to my apache server log.
Trying to write a conditional with jQuery that basically states, if div.gathering does not
I've taken up learning vb.net having come from a C# background. I was trying

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.