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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:48:45+00:00 2026-05-27T11:48:45+00:00

The first part of this Frankenstein-ed Java works perfectly, however the second part outputs

  • 0

The first part of this “Frankenstein-ed” Java works perfectly, however the second part outputs some jumbled nonsense. So the variable of result will be my input from the user. I had to first UpperCase the string before I did the parsing for some dumb reason, it’s hard when you come from the Database/Analysis background and know you do something in seconds and not get an error… I gave credit where credit is due within the code…

myfile.txt —> [Ljava.lang.String;@19821f

  import java.io.*;
/*http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29*/

  public class StringParser {

  public static void main (String arg[])
     throws FileNotFoundException {
String result = "eggs toast bacon bacon butter ice beer".toUpperCase();
  String[] resultU = result.split("\\s");
  String[] y = resultU;

  {
for (int x=0; x< resultU.length; x++)



    System.out.println(resultU[x]);


  /*http://www.javacoffeebreak.com/java103/java103.html#output*/

            FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object

            try
            {
                    // Create a new file output stream
                    // connected to "myfile.txt"
                    out = new FileOutputStream("myfile.txt");

                    // Connect print stream to the output stream
                    p = new PrintStream( out );

                    p.println (resultU);

                    p.close();
            }
            catch (Exception e)
            {
                    System.err.println ("Error writing to 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-05-27T11:48:45+00:00Added an answer on May 27, 2026 at 11:48 am

    Do you realize you’re overwriting the same file for each element in your array?

    You should use

    out = new FileOutputStream("myfile.txt", true); // appends to existing file
    

    As well as printing the actual element, not the String representation of the whole array

    p.println(resultU[x]);  // resultU without index prints the whole array - yuk!
    

    Although you should probably update your code to only create the output File once and just write each element of the array to the same output stream, as the current method is a bit inefficient.

    Something like

    public static void main(String[] args) {
    
        String result = "eggs toast bacon bacon butter ice beer".toUpperCase();
    
        PrintStream p = null;
    
        try {
            p = new PrintStream(new FileOutputStream("myfile.txt"));
    
            for (String s : result.split("\\s")) {
                p.println(s);
                p.flush(); // probably not necessary
            }
    
        } catch (Exception e) {
            e.printStackTrace(); // should really use a logger instead!
        } finally {
            try {
                p.close(); // wouldn't need this in Java 7!
            } catch (Exception e) {
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First part of this question will look trivial, but the point is in second
First part of the code works OK while the second (commented) does not. It
This is a two-part question: First, I am interested to know what the best
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is kinda....a two part question. The first one is much more important than
This is really a three-part question, but I've answered the first question myself: I'm
I just recorded some song and cut it into two parts. The first part
I know the first part of this question has been asked before , but
I have asked the first part of this question before, but with no answer
I have this mysql query using PHP and first part is working fine, but

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.