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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:01:24+00:00 2026-06-18T06:01:24+00:00

So I take in a line from a .txt file and turn it into

  • 0

So I take in a line from a .txt file and turn it into a string. I would like to split the string up by |, but I also have spaces before and after it that is messing with the code, here is what I have so far:

File file = new File(fileLocation);
Scanner sc = new Scanner(file);
String line;
String[] words;
while(sc.hasNext()){
    line = sc.next();
    words = line.split("\\|");
    this.german.add(words[0]);
    this.english.add(words[1]);
}

An example line would be something like: in blue|in blau

I would also like to keep the spaces.

The .txt file would be:

in Rot|in red

in Blau|in blue

in Grun|in green

in Gelb|in Yellow

It would add all the items on the left of the | to the german list, and all of the ones on the right to the english list.

Ah, figured it out, the sc.next() is the next String, not the next line, I replaced it with sc.nextLine() and everything worked, thanks.

  • 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-18T06:01:25+00:00Added an answer on June 18, 2026 at 6:01 am

    Call

    line.replaceAll(" ", "");
    

    beforehand; this will get rid of all the spaces. If you only want leading and trailing spaces from the split strings removed, use

    words[i].trim()
    

    instead.

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

Sidebar

Related Questions

I would like to make something like tryruby.org . I take a line from
I have file A2.txt with coordinate x1,y1,x2,y2 in every line like below : 204
I would like to take a specific part of the last line of a
I would like to use Perl to take a previously generated SPSS syntax file
I would like to construct a mechanism that constructs different facts depending from txt
How we can take input from the file in the Eclipse? Just like we
In my project regression settings, output file have statement like diff between foo.txt and
I'm reading from a .txt file and have four scores for each person, i
Is there anyway we can take input from command line in Objective-C, the way
I am trying to take a line of text like 13) Check for orphaned

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.