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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:43:36+00:00 2026-06-13T16:43:36+00:00

I have a problem that’s breaking my mind. I have a .txt file that

  • 0

I have a problem that’s breaking my mind. I have a .txt file that looks like

fiat,regata,15*renault,seiscientos,25*

In my code I have this

       Scanner sc=new Scanner(new File("coches.txt");
        sc.useDelimiter("[,*]");
        while(sc.hasNext()){   
            marca=new StringBuffer(sc.next());
            modelo=new StringBuffer(sc.next());
            marca.setLength(10);
            modelo.setLength(10);
            edad=sc.nextInt();

            coche=new Coche(marca.toString(),modelo.toString(),edad);
            coches.add(coche);
        }

The problem here is that the While loop is working three times, so the third time marca=\n and It stops with a java.util.NoSuchElementException. So, How can I use my delimiter to stop de loop in the last * and avoid It to enter in that extra/problematic time?.

I already tried things like

while(sc.next!="\n")

I also triyed this and doesn’t work

sc.useDelimiter(“[,\*\n]”);

SOLVED!!!

I finally found the solution, in part thanks to the advice of user1542723. The solution
is:

String linea;
String [] registros,campos;    
File f=new File("coches.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);//ALL this need Try Catch that I'm not posting

while((linea=br.readLine())!=null){
        registros=linea.split("\\*");
    }
    for (int i = 0; i < registros.length; i++) {
        campos=registros[i].split(",");
        marca=campos[0];
        modelo=campos[1];
        edad=Integer.parseInt(campos[2]);//that's an Int, edad means Age

        coche=new Coche(marca.toString(),modelo.toString(),edad);
        coches.add(coche);
    }
}

Thank you everybody who helped me.

  • 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-13T16:43:38+00:00Added an answer on June 13, 2026 at 4:43 pm

    You may want to escape the star in your regex:

    sc.useDelimiter("[,\\*]");

    Because

    "[,*]" means , zero or more times and "[,\\*]" means , or *.

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

Sidebar

Related Questions

I have a problem that has been driving me batty. I have code that
I have a problem that I would like to solve using Django's ORM. I
I have one problem that I can't explain. Here is the code in main
I have the problem that I like to automagically delete documents in my couch,
I have problem that im trying to get solve for like week. My goal
I have problem that is in my code i create splash screen for a
I have a problem that seems like its a result of a deadlock-situation. Whe
i have this problem that i can't solve for days now...here is my code.
I have a problem that I would like to share. The context is a
In a couple of scripts that I use I have problem that is intermittent.

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.