My Printwriter method is not writing to a text file. the code runs fine it displays the text on screen but nothing appears in the text file.

public void run() throws IOException
    {
    
       Scanner sc = new Scanner(System.in);
       boolean cont = true;
       while (cont)
       {
        System.out.println("Enter your line text");
       
        String line = sc.nextLine();
        if ("*".equals(line))
        {
            cont = false;
        }
        String result = shorthand(line);
        System.out.println(result);
        PrintWriter pw = new PrintWriter(new FileWriter("outfile.txt"));
        pw.println(result);  
        pw.close();
      }
    }

Edited 6 Years Ago by luvthug: n/a

What I wan to acheive is.

the that is typed in aswell as it should display on screen and should be written to a file and each string should be on a seprate line. So * the asterik terminates the program but thats the only thing which appears in the file

What I wan to acheive is.

the that is typed in aswell as it should display on screen and should be written to a file and each string should be on a seprate line. So * the asterik terminates the program but thats the only thing which appears in the file

What? Use good grammar.

import java.io.*;
import java.util.*;


public class Shorthand_1
{    
   
    public boolean isVowel(char c)
   {
       
       if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'|| c == 'E'||c == 'I'|| c == 'O'|| c == 'U')
       {
           return true;
       }
       else
       {
           return false;
       }
   }
    
   public String shorthand(String in)  
   { 
    StringBuilder vowel = new StringBuilder(); 
 
    for (int i=0; i < in.length(); i++) 
    { 
      if (isVowel(in.charAt(i)) == false) 
      {  
        vowel.append(in.charAt(i));  
      } 
    }  
     return vowel.toString();  
   } 

    
    
    public void run() throws IOException
    {
    
        Scanner sc = new Scanner(System.in);
       boolean cont = true;
       PrintWriter pw = new PrintWriter ("outfile.txt");
       while (cont)
       {
        System.out.println("Enter your line text");
       
        String line = sc.nextLine();
        if ("*".equals(line))
        {
            cont = false;
        }
        String result = shorthand(line);
        System.out.println(result);

        pw.println(result);
        
       }
    }  
}

This is what the question is asking for.

Modify the run method so that now the compressed string is not only printed in the output window, but also written to a file called outfile.txt in the project directory. Each compressed string should be on a separate line in the file. When * entered the code should terminate but * should not be written in the file

Edited 6 Years Ago by luvthug: n/a

This is what the question is asking for.

Modify the run method so that now the compressed string is not only printed in the output window, but also written to a file called outfile.txt in the project directory. Each compressed string should be on a separate line in the file. When * entered the code should terminate but * should not be written in the file

the code I posted in the other thread already does that, but you failed to copy it correctly:

if ("*".equals(line)) {
 cont = false;
} else {
   // do stuff with the line
}

I would suggest that you initialize the Printer outside of the while. With your way with each loop you crerate a new one and overwrite the old entries. Also it is not very efficient. Have you tried putting the full path of the file?

Also don't forget to close the Printer outside the while

Edited 6 Years Ago by javaAddict: n/a

Yes boss I have been at it since the last time you helped with the code. It works perfectly without the printwrite stuff. I have tried intializing the PrintWriter outside of while loop but no difference still only print *.

I have just what you have mentioned closing the pw.close outside the while loop , full path . the problem is its only printing * .

Isn't there any onw who can help with this question.


Modify the run method so that now the compressed string is not only printed in the output window, but also written to a file called outfile.txt in the project directory. Each compressed string should be on a separate line in the file. When * entered the code should terminate but * should not be written in the file

Isn't there any onw who can help with this question.


Modify the run method so that now the compressed string is not only printed in the output window, but also written to a file called outfile.txt in the project directory. Each compressed string should be on a separate line in the file. When * entered the code should terminate but * should not be written in the file

Post your latest code.

And don't yell. We are not your slaves to run on demand and fix your code. We have jobs and deadlines on our own. We are not forced to answer to you. We do it for free in out own convenience.

Hi I appoogise my intention was not to shout or demand an answer, neither I think I have slaves, I am just asking for help. I have tried what you suggested earier but it did not work.

here is the code:

import java.io.*;
import java.util.*;


public class Shorthand_1
{    
   
    public boolean isVowel(char c)
   {
       
       if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'|| c == 'E'||c == 'I'|| c == 'O'|| c == 'U')
       {
           return true;
       }
       else
       {
           return false;
       }
   }
    
   public String shorthand(String in)  
   { 
    StringBuilder vowel = new StringBuilder(); 
 
    for (int i=0; i < in.length(); i++) 
    { 
      if (isVowel(in.charAt(i)) == false) 
      {  
        vowel.append(in.charAt(i));  
      } 
    }  
     return vowel.toString();  
   } 

    
    
    public void run() throws IOException
    {
    
        Scanner sc = new Scanner(System.in);
       boolean cont = true;
       PrintWriter pw = new PrintWriter (new FileWriter("outfile.txt"));
      
       while (cont)
       {
        System.out.println("Enter your line text");
       
        String line = sc.nextLine();
       
        if ("*".equals(line))
        {
            cont = false;
        }
        
        String result = shorthand(line);
        System.out.println(result);
        pw.println(result);
       
       
       }
      pw.close();
    }  
}

No you didn't try my suggestion, nor you understood it.
Look at post Number #8

Even without that post, if you don't want to write the '*' then don't write it. You decide what is written and what is not, when you call the methods

Then hos is the program going to terminate if * is not entered, which is what the questions ask for

Then hos is the program going to terminate if * is not entered, which is what the questions ask for

Can you repeat that, I didn't understood it

What I am trying to say is The question says when * is entered the program should terminate. if i remove this then how is the program going to end.

What I am trying to say is The question says when * is entered the program should terminate. if i remove this then how is the program going to end.

If you remove what? The answer has been given to you. in previous posts

And like I said: Using if statements, you can say when you want the loop to stop and what you want to be saved

Im not to sure (beginner), but can you just do the following:

File file = new File("outfile.txt")
#
String result = shorthand(line);
#
System.out.println(result);
#
PrintWriter pw = new PrintWriter(file);
#
pw.println(result);
#
pw.close();
This article has been dead for over six months. Start a new discussion instead.