Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Appending to EOF error

Posted on 2004-10-08
17
Medium Priority
?
519 Views
Last Modified: 2008-03-04
I am trying to add some strings to EOF my code is as below, but I get an error saying can't convert FileOutPutStream to String




      try
            {
                  FileOutputStream newTicker = new FileOutputStream("nasdaq");
                  PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
                  out.println(ticker);
                  out.close();
            }
      
            catch (IOException e)
            {
                  System.err.println(e.getMessage());
                  System.exit(1);
            }
0
Comment
Question by:izomax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +3
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12263460
>>out.println(ticker);

Would appear 'ticker' is not of type String. It needs to be
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12263485
Points to CEHJ, you simply need to do this:

String ticker = "Something";
// Then the rest of your above code here. :)

[r.D]
0
 

Author Comment

by:izomax
ID: 12263539
Hi

The complie error is in the newTicker.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12263545
Please show where 'ticker' is declared
0
 

Author Comment

by:izomax
ID: 12263551
javac -classpath /cad2/corba/JOB-3.3.2/ob/lib/OB.jar:. broker2/*.java
broker2/OnlineBroker.java:116: Incompatible type for constructor. Can't convert java.io.FileOutputStream to java.lang.String.
                        PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
                                                                         ^
broker2/OnlineBroker.java:117: Variable ticker may not have been initialized.
                        out.println(ticker);
                                    ^
0
 

Author Comment

by:izomax
ID: 12263556
   public void addSymbol(String symbol) throws broker2.BrokerPackage.BrokerErrorException
    {
      String ref = null;
      String ticker;
      String value;
      int positionSpace;
      boolean foundSymbol = false;
      
      try
      {
            FileInputStream file = new FileInputStream("nasdaq");
            BufferedReader in =   new BufferedReader(new InputStreamReader(file));
            
            while((ref = in.readLine())!=null)
            {
            
                  positionSpace = ref.indexOf(' ');
                  ticker = ref.substring(0, positionSpace);
                  value = ref.substring(positionSpace+1);            
            
                  if(ticker.equalsIgnoreCase(symbol))
                  {
                        broker2.BrokerPackage.BrokerErrorException err = new
                              broker2.BrokerPackage.BrokerErrorException(broker2.BrokerPackage.BrokerErrors.SymbolExists);
                        throw err;
                  }
                  else
                  {
                        foundSymbol = false;
                        
                        //FileOutputStream newTicker = new FileOutputStream("nasdaq");
                        //System.out.println("adding symbol\n");
                        //PrintWriter out = new PrintWriter(new FileWriter("nasdaq", true));
                        //out.println(ticker);
                        //out.close();
                  }
                  

            }
            
            if ( foundSymbol == false)
            {
      
            //add symbol to the EOF
            try
            {
                  FileOutputStream newTicker = new FileOutputStream("nasdaq");
                  PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
                  out.println(ticker);
                  out.close();
            }
      
            catch (IOException e)
            {
                  System.err.println(e.getMessage());
                  System.exit(1);
            }
      }      
            
            
            
            file.close();
      }
      
      catch (IOException e)
      {
            System.err.println(e.getMessage());
            System.exit(1);
      }
      
      /*if ( foundSymbol == false)
      {
      
            //add symbol to the EOF
            try
            {
                  FileOutputStream newTicker = new FileOutputStream("nasdaq");
                  PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
                  out.println(ticker);
                  out.close();
            }
      
            catch (IOException e)
            {
                  System.err.println(e.getMessage());
                  System.exit(1);
            }
      }*/
      
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12263578
Please say what happens when you do, instead of

>>PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));


java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(newTicker, true));




0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12263581
Oh and

java.io.FileOutputStream newTicker = new java.io.FileOutputStream("nasdaq");
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12263664
As for ticker not having been initialized, make the declaration:
String ticker = "";
or
String ticker = null;
0
 

Author Comment

by:izomax
ID: 12263957
Hi

I get the same error:

javac -classpath /cad2/corba/JOB-3.3.2/ob/lib/OB.jar:. broker2/*.java
broker2/OnlineBroker.java:116: Incompatible type for constructor. Can't convert java.io.FileOutputStream to java.lang.String.
                        java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(newTicker, true));
 
    ^
0
 

Author Comment

by:izomax
ID: 12263959
Hi

I get the same error:

javac -classpath /cad2/corba/JOB-3.3.2/ob/lib/OB.jar:. broker2/*.java
broker2/OnlineBroker.java:116: Incompatible type for constructor. Can't convert java.io.FileOutputStream to java.lang.String.
                        java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(newTicker, true));
 
    ^
0
 

Author Comment

by:izomax
ID: 12263960
Hi

I get the same error:

javac -classpath /cad2/corba/JOB-3.3.2/ob/lib/OB.jar:. broker2/*.java
broker2/OnlineBroker.java:116: Incompatible type for constructor. Can't convert java.io.FileOutputStream to java.lang.String.
                        java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(newTicker, true));
 
    ^
0
 

Author Comment

by:izomax
ID: 12263961
Hi

I get the same error:

javac -classpath /cad2/corba/JOB-3.3.2/ob/lib/OB.jar:. broker2/*.java
broker2/OnlineBroker.java:116: Incompatible type for constructor. Can't convert java.io.FileOutputStream to java.lang.String.
                        java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(newTicker, true));
 
    ^
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12264015
Once is enough you know... :P

Try
PrintWriter out = new PrintWriter(new FileOutputStream("nasdaq"), true);
0
 
LVL 92

Expert Comment

by:objects
ID: 12273071
         try
          {
               File newTicker = new File("nasdaq");
               PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
               out.println(ticker);
               out.close();
          }
     
          catch (IOException e)
          {
               System.err.println(e.getMessage());
               System.exit(1);
          }
0
 
LVL 1

Accepted Solution

by:
IOException earned 2000 total points
ID: 12311121
I think The problem lies in the fact that
there is no Overloaded constructor for FileWriter in java which takes FileOutputStream as constructor...

so may wanna modify the statement to directly open the file in FileWriter , instead of Plugging the file into a stream and then plugging Stream to FileWriter
instead of
File.nasdaq->FileOutputStream.newTicker->FileWriter->PrintWriter
eliminate the underlined part....
So ur code becomes something like this
Code:

 //add symbol to the EOF
try
{
//FileOutputStream newTicker = new FileOutputStream("nasdaq");
// FileOutputStream not required

//PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
// Invalid FileWriter Constructor, Valid constructors are the ones that take File,String,FileDescription ).
PrintWriter out = new PrintWriter(new FileWriter("nasdaq", true));

out.println(ticker);
out.close();
}
 


See if this helps ya!!
(I copy pasted ur prog, hacked it a lil to make the snippet work independantly and got it working this way... Hope this helps....)

Now if u r interested here is the complete Code Base that a worked on...
See it for urself...

Code:

import java.io.*;
import java.io.File;




public class javaFile
{
public javaFile()
{
}
public void addSymbol(String symbol) throws IOException
{
   String ref = null;
   String ticker = symbol;
   String value;
   int positionSpace;
   boolean foundSymbol = false;

   try
   {
      FileInputStream file = new FileInputStream("nasdaq.txt");
      BufferedReader in = new BufferedReader(new InputStreamReader(file));

/*      while((ref = in.readLine())!=null)
      {

         positionSpace = ref.indexOf(' ');
         ticker = ref.substring(0, positionSpace);
         value = ref.substring(positionSpace+1);

         if(ticker.equalsIgnoreCase(symbol))
         {
            broker2.BrokerPackage.BrokerErrorException err = new
            broker2.BrokerPackage.BrokerErrorException(broker2.BrokerPackage.BrokerErrors.SymbolExists);
            throw err;
         }
         else
         {
            foundSymbol = false;

            //FileOutputStream newTicker = new FileOutputStream("nasdaq");
            //System.out.println("adding symbol\n");
            //PrintWriter out = new PrintWriter(new FileWriter("nasdaq", true));
            //out.println(ticker);
            //out.close();
         }


      }

      if ( foundSymbol == false)
      {
*/
         //add symbol to the EOF
         try
         {
            //FileOutputStream newTicker = new FileOutputStream("nasdaq.txt");
             
            PrintWriter out = new PrintWriter(new FileWriter("nasdaq.txt", true));
            out.println(ticker);
            out.close();
         }

         catch (IOException e)
         {
            System.err.println(e.getMessage());
            System.exit(1);
         }
//      }



      file.close();
   }

   catch (IOException e)
   {
      System.err.println(e.getMessage());
      System.exit(1);
   }

   /*if ( foundSymbol == false)
   {

      //add symbol to the EOF
      try
      {
         FileOutputStream newTicker = new FileOutputStream("nasdaq");
         PrintWriter out = new PrintWriter(new FileWriter(newTicker, true));
         out.println(ticker);
         out.close();
      }

      catch (IOException e)
      {
         System.err.println(e.getMessage());
         System.exit(1);
      }
   }*/


}
      public static void main(String args[])
      {
         javaFile x= new javaFile();
         try
         {
            x.addSymbol("cool maan jurenamo");
         }
         catch(IOException e)
         {
            System.out.println("Oops!! "+e);
         }
      }

}
 


(I created a temp file called nasdaq.txt and then worked on it.. it worked....)
0
 
LVL 92

Expert Comment

by:objects
ID: 12335789
thats basically the same as I posted above :)
just uses a filename, instead of a File object. end result it still the same.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question