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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:27:46+00:00 2026-06-17T10:27:46+00:00

My problem could be solved if Scanner class had previous() method on it. I

  • 0

My problem could be solved if Scanner class had previous() method on it. I am asking this question to know if there are any methods to achieve this functionality.

Input:
a file with contents like

a,1
a,2
a,3
b,1
c,1
c,2
c,3
c,4
d,1
d,2
d,3
e,1
f,1

I need to create a list of all lines that has same alphabet.

try {
            Scanner scanner = new Scanner(new File(fileName));
            List<String> procList = null;
            String line =null;
            while (scanner.hasNextLine()){
                line = scanner.nextLine();
                System.out.println(line);
                String[] sParts = line.split(",");
                procList = new ArrayList<String>();
                procList.add(line);
                boolean isSamealpha = true;
                while(isSamealpha){
                    String s1 = scanner.nextLine();
                    if (s1.contains(sParts[0])){
                        procList.add(s1);
                    }else{
                        isSamealpha = false;
                        System.out.println(procList);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

I get output like

a,1
[a,1, a,2, a,3]
c,1
[c,1, c,2, c,3, c,4]
d,2
[d,2, d,3]
f,1
[f,1]

As you can see it missed list for b and e. If I has scanner.previous() method, I would have put it in else of second while loop. Because there is no previous method, I am stuck.

Please let me know if there are any methods I can use. I can’t use FileUtils.readLines() because its a 3GB file and I don’t want to use my java memory to store all the file.

  • 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-17T10:27:47+00:00Added an answer on June 17, 2026 at 10:27 am

    Below is some sample code that should help. (I only typed this; I did no checking.)

    Scanner scanner = new Scanner(new File(fileName));
    List<String> procList = null;
    String line = null;
    String previousAlpha = null;
    while (scanner.hasNextLine()){
        line = scanner.nextLine();
    
        if (previousAlpha == null) {
            // very first line in the file
            procList = new ArrayList<String>();
            procList.add(line);
            System.out.println(line);
            previousAlpha = line.split(",")[0];
        }
        else if (line.contains(previousAlpha)) {
            // same letter as before
            procList.add(line);
        }
        else {
            // new letter, but not the very first
            // line
            System.out.println(procList);
    
            procList = new ArrayList<String>();
            procList.add(line);
            System.out.println(line);
            previousAlpha = line.split(",")[0];
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any good practice for this? I wish I could solve the problem
I was wondering if this rope bridge problem could be solved with a graphing
I have an complex problem, but it could be solved via this little problem.
I have an itching problem I know could be solved using many different ways,
i have this problem: ms-access could not delete and i found a potential solution
Penz says that the problem could be solved by Multios and coproc features in
Today, an interviewer asked me this question. My immediate response was that we could
all, I have searched for this problem for long time and tried different methods.
I'm troubleshooting a permissions problem which could be solved if all files in Apache's
I have a strange problem that I could not solve. When I try to

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.