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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:34:35+00:00 2026-05-23T02:34:35+00:00

I have a text file containing the following content: 0 12 1 15 2

  • 0

I have a text file containing the following content:

0 12
1 15
2 6
3 4
4 3
5 6
6 12
7 8
8 8
9 9
10 13

I want to read these integers from a txt file and save the two columns into two different arrays in Java.

Thanks to aioobe for his nice answer for the first part.

Now I want to develop it in this way:

  1. Write a method called occurrence, that take a number as an input and write the number of occurrence that this number has.

  2. Write another method called occurrences, that didn’t have any input, but as output it gives the number that has the major number of occurrences (in the second column) in the file.

  3. At last, the Main program will ask the user to write a number from 1 to 3.

1= method that, from an input number (that is the number in the first column) gives back the associated number in the second column.

2= the first occurrence method (the one with input)

3= the second occurrence method (the one without input)

I wrote the code but there is some errors(about passing the array list to a method) and I need your help about that.
I am a JAVA novice, So if you feel that the code is not appropriate, please make the required changes.
This is my final code:

import java.util.*; //importing some java classes
import java.lang.*;
import java.io.*;

public class list {

    public static void main(String[] args) {

        // Read the text file and store them into two arrays:
        try {
            List<Integer> column1 = new ArrayList<Integer>();   // Defining an integer Array List
            List<Integer> column2 = new ArrayList<Integer>();   // Defining an integer Array List

            Scanner myfile = new Scanner(new FileReader("c:/java/data.txt")); // Reading file using Scanner

            while (myfile.hasNext()) {          // Read file content using a while loop
                column1.add(myfile.nextInt());      // Store the first integer into the first array list
                column2.add(myfile.nextInt());      // Store the next integer into the second array list
            }

            myfile.close(); // close the file

            System.out.println("column 1 elements are:\n" + column1);  // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
            System.out.println("column 2 elements are:\n" + column2);  // [12, 15, 6, 4, 3, 6, 12, 8, 8, 9, 13]

            //Getting a number(1-3) from user:
            Scanner cases = new Scanner(System.in);
            System.out.println("Enter a number between 1-3: "); 
            int num = cases.nextInt();

            switch (num) {
                case 1:
                    Scanner case1 = new Scanner(System.in);
                    System.out.println("Enter a number from first column to see how many occurrences it has: "); 
                    int numb = case1.nextInt();
                    System.out.println(column2.get(numb));
                    break;
                case 2:
                    occurrence(column2.toArray());
                    break;
                case 3:
                    occurrences(column2.toArray());
                    break;              
                default: System.out.println("the number is not 1 or 2 or 3!"); 
            }

        } catch (Exception e) {     // we defined it just in the case of error
            e.printStackTrace();    // shows the error
        }

    } // End of MAIN


    public void occurrence(int[] arg) { // Defining occurrence method

        int count = 0;

        //Getting a number from user input:
        Scanner userin = new Scanner(System.in);
        System.out.println("Enter an integer number: "); 
        int number = userin.nextInt();        

        // Finding the occurrences:
        for (int i = 0; i < arg.length; i++)
            if (arg[i] == number) count++;

        System.out.println( number + " is repeated " + count + " times in the second column.");
    } // End of occurrence method


    public void occurrences(int[] arg) {    // Defining occurrenceS method

        int max = 0;

        // Finding the maximum occurrences:
        for (int i = 1; i < arg.length; i++)
            if (arg[i] > arg[max]) max = i;
            System.out.println( max + " is the most repeated  number." );
    } // End of occurrenceS method


}
  • 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-23T02:34:36+00:00Added an answer on May 23, 2026 at 2:34 am

    you are passing an Object[] while your method expects an int[]

    (note that toArray() returns an Object[])

    also, occurrence() is an instance method, while main is static, you will need to change occurrence() to be a static method as well, or create an instance of list.

    so methods signature will be:

    public static void occurrences(Integer[] arg) 
    
    public static void occurrence(Integer[] arg)
    

    and methods calls will be:

    Integer[] temp = new Integer[column2.size()];
                switch (num) {
                    case 1:
                        Scanner case1 = new Scanner(System.in);
                        System.out.println("Enter a number from first column to see how many occurrences it has: "); 
                        int numb = case1.nextInt();
                        System.out.println(column2.get(numb));
                        break;
                    case 2:
                        occurrence(column2.toArray(temp));
                        break;
                    case 3:
                        occurrences(column2.toArray(temp));
                        break;              
                    default: System.out.println("the number is not 1 or 2 or 3!"); 
                }
    

    p.s. this is not related to your question, but there is a strong java convention that a class name starts with a capital letter.

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

Sidebar

Related Questions

I have a text file containing the following content: 0 12 1 15 2
I have a text file containing the data in following format 12345 Abdt3 hy45d
I have a text file containing words separated by newline , like the following
I have the following code reading a file containing unicode text (Japanese). File f
I have a text file containing strings to encrypt. These strings are indicated by
I have a text file containing 5 columns of data. The first column contains
I have a text file containing two or more types of lines. I would
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have to filter a text file filter.tmp containing two types of lines, this
I have a simple text file containing some CSV with the following structure: @Parent1_Field1,

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.