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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:59:32+00:00 2026-06-10T22:59:32+00:00

I need help on this code. The input is stored in a text file

  • 0

I need help on this code.
The input is stored in a text file like this:

First:3 6 
Secon:3 9 

The output is:

First:3 6 

The Maximum number is6

The sum is9


Secon:3 9 


The Maximum number is9

The sum is12

But my desired output should be :

    The Maximum number is6
    The sum is12 

So,it applies Math.max() on the first line only.
And,it add the second line numbers.

Please help.

import java.io.*;

public class cape 
{
 public static void main(String args[])
  {
  try{

  FileInputStream fstream = new FileInputStream("C:/Users/PC4599/Desktop/cape.txt"); // Open the text file.
  DataInputStream in = new DataInputStream(fstream);// Get the object of DataInputStream
  BufferedReader br = new BufferedReader(new InputStreamReader(in));

  String MyString;


  //Read File Line By Line
  while ((MyString = br.readLine()) != null) 
  {

  // Print the content on the console
  System.out.println (MyString);


  Character c = new Character(MyString.charAt(6));
  Character c2 = new Character(MyString.charAt(8));



  String s = c.toString();
  String s2 = c2.toString(); 


  int i = Integer.parseInt(s);
  int i2 = Integer.parseInt(s2);

    int sum=i+i2;
  System.out.println("The Maximum number is"+ Math.max(i, i2));
  System.out.println("The sum is"+ sum);

  }
  //Close the input stream
  in.close();
  }catch (Exception e){
   //Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}
  • 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-10T22:59:34+00:00Added an answer on June 10, 2026 at 10:59 pm

    You could just do this:

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("test.txt")));
        String[] nums1 = br.readLine().split(":")[1].split(" ");
        String[] nums2 = br.readLine().split(":")[1].split(" ");
        br.close();
        System.out.println("The maximum number is " + Math.max(Integer.parseInt(nums1[0]), Integer.parseInt(nums1[1])));
        System.out.println("The sum is " + (Integer.parseInt(nums2[0]) + Integer.parseInt(nums2[1])));    
    }
    

    If your file has more than just 2 lines, it shouldn’t be hard to employ a similar method using a loop.

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

Sidebar

Related Questions

i have this code, and need help with the logic end. I would like
I need to put $input into the file spelled.. but this code leaves the
Kind of need help understanding what this code actually outputs. Does it out put
Sorry to bother again, but I really need help transforming this Python2 code into
Please i need help, this code below works fine on my localhost, php5.3+ but
I'm not good at t-sql, so I need help. I have this code I
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help to use jConfirm with this existing code (php & Jquery &
This is a code for calculating the Bowling Score,I need help in fixing this
I really need help this time - Joomla 2.5 and latest K2. I'm using

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.