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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:37:23+00:00 2026-05-14T08:37:23+00:00

I am not an experienced Java programmer and i’m trying to write some text

  • 0

I am not an experienced Java programmer and i’m trying to write some text to a file and then read it with Scanner. I know there are lots of ways of doing this, but i want to write records to file with delimiters, then read the pieces.

The problem is so small. When I look the output some printing isn’t seen(shown in below). I mean the bold line in the Output that is only written “Scanner“. I would be appreciated if anyone can answer why “String: ” isn’t seen there. (Please answer just what i ask)

I couldn’t understand if it is a simple printing problem or a line end problem with “\r\n”.

Here is the code:


    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

public class Tmp {
    public static void main(String args[]) throws IOException {
        int i;
        boolean b;
        String str;

        FileWriter fout = new FileWriter("test.txt");
        fout.write("Testing|10|true|two|false\r\n");
        fout.write("Scanner|12|one|true|");
        fout.close();

        FileReader fin = new FileReader("Test.txt");

        Scanner src = new Scanner(fin).useDelimiter("[|\\*]");

        while (src.hasNext()) {
            if (src.hasNextInt()) {
                i = src.nextInt();
                System.out.println("int: " + i);
            } else if (src.hasNextBoolean()) {
                b = src.nextBoolean();
                System.out.println("boolean: " + b);
            } else {
                str = src.next();
                System.out.println("String: " + str);
            }
        }

        fin.close();
    }
}   

Here is the output:


String: Testing
int: 10
boolean: true
String: two
String: false
Scanner
int: 12
String: one
boolean: true

  • 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-14T08:37:24+00:00Added an answer on May 14, 2026 at 8:37 am

    The problem is that you are writing out the String “String: ” and then writing out control character \r, or carriage return, and then writing out the contents.

    The following version should work a bit better for you:

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing|10|true|two|false\n");
    fout.write("Scanner|12|one|true|");
    fout.close();
    FileReader fin = new FileReader("test.txt");
    Scanner src = new Scanner(fin).useDelimiter("[|\n]");
    

    To really see what I am talking about with the \r, you should change your original program so the print code looks like this:

      } else {
        str = src.next().trim();
        str = str.replace('\n', '_');
        str = str.replace('\r', '_');
        System.out.println("String: " + str);
      }
    

    You should see the output:

    String: false__Scanner

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

Sidebar

Related Questions

I'm not the worlds most experienced Java programmer, and I'm trying to create a
I was just trying out some code, not a experienced coder. I implemented(at least
I'm an experienced C++/.NET/Java Windows/web programmer trying to learn (Common) Lisp. I'm reading Practical
Pardon my newbie-ness to Java as I am not experienced enough to know the
New programmer here with only some minor Java experience trying my hand at writing
Hei there, I'm not experienced at all in C++ as I need to start
I am not much experienced in boost::asio . I've some pretty basic questions. Do
I'm not very experienced in C#, done some small stuff with it, but it's
I'm not an experienced C++ programmer and I'm having problems compiling. I've got a
I'm an experienced Java programmer that for the last two years have programmed for

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.