Results 1 to 6 of 6
  1. #1
    janroz is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Post reading and writing string on notepad file

    the question is i need to input 10 integers and then it should be square by its number using reading and writing string fron notepad file. here is my code by there are a lot of problem..

    Java Code:
    import java.io.*;
    
    class Act02FileWriter {
    	
    	public static void main(String args[]) throws Exception {
    		
    
    	String fileName = "squares.txt";
    	PrintWriter print = null;
    	int integers;
    	
    	BufferedReader Data=new BufferedReader(new InputStreamReader(System.in));
    	String ans;
    	
    	System.out.println("Enter 10 integers :");
    	int z = Integer.parseInt(Data.readLine());
    
    
    	
    	do{	
    	try{
    	print = new PrintWriter (new BufferedWriter (new FileWriter ("squares.txt")));
    	
    	for (int i=1; i <= 10; i++) 
    		{ 
    		z = Integer.parseInt(readLine());	
    		int x = z * z;
    		print.println(z + " ");
    	
    	}
    
    	catch(IOException iox) {
    	System.out.println("Problem writing " + fileName);
    	
    	}
    	print.println("");
    	print.close();
    	}	
    			
    
    String fileName = "squares.txt";
    String line;
    try{
    	
    BufferedReader in=new BufferedReader( new FileReader(fileName, true));
    line = in.readLine();
    while (Line != null)	
    		
    {
    
    System.out.println(Line);
    line = in.readLine();
    }
    in.close();
    } catch ( IOException iox) {
    	System.out.println("Problem reading " + fileName );
    }
    
    
    	ans = Data.readLine();	
    	System.out.print("");
    
    } while (ans.equalsIgnoreCase("yes"));
    
    
    	}	
    }
    and these are the errors: --------------------Configuration: <Default>--------------------
    Z:\prog131\Act02FileWriter.java:32: 'catch' without 'try'
    catch(IOException iox) {
    ^
    Z:\prog131\Act02FileWriter.java:32: ')' expected
    catch(IOException iox) {
    ^
    Z:\prog131\Act02FileWriter.java:32: not a statement
    catch(IOException iox) {
    ^
    Z:\prog131\Act02FileWriter.java:32: ';' expected
    catch(IOException iox) {
    ^
    Z:\prog131\Act02FileWriter.java:21: 'try' without 'catch' or 'finally'
    try{
    ^
    5 errors

    Process completed.
    Last edited by Eranga; 01-17-2012 at 12:41 PM. Reason: code tags added

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: reading and writing string on notepad file

    Java Code:
    import java.io.*;
    
    class Act02FileWriter {
    	
    	public static void main(String args[]) throws Exception {
    		
    
    	String fileName = "squares.txt";
    	PrintWriter print = null;
    	int integers;
    	
    	BufferedReader Data=new BufferedReader(new InputStreamReader(System.in));
    	String ans;
    	
    	System.out.println("Enter 10 integers :");
    	int z = Integer.parseInt(Data.readLine());
    
    
    	
    	do{	
    	try{
    	print = new PrintWriter (new BufferedWriter (new FileWriter ("squares.txt")));
    	
    	for (int i=1; i <= 10; i++) 
    		{ 
    		z = Integer.parseInt(readLine());	
    		int x = z * z;
    		print.println(z + " ");
    	
    	}
    
    	catch(IOException iox) {
    	System.out.println("Problem writing " + fileName);
    	
    	}
    	print.println("");
    	print.close();
    	}	
    			
    
    String fileName = "squares.txt";
    String line;
    try{
    	
    BufferedReader in=new BufferedReader( new FileReader(fileName, true));
    line = in.readLine();
    while (Line != null)	
    		
    {
    
    System.out.println(Line);
    line = in.readLine();
    }
    in.close();
    } catch ( IOException iox) {
    	System.out.println("Problem reading " + fileName );
    }
    
    
    	ans = Data.readLine();	
    	System.out.print("");
    
    } while (ans.equalsIgnoreCase("yes"));
    
    
    	}	
    }
    Please use code tags when posting code.
    Also indent your code properly, as this looks to me like you've missed a bracket or two somewhere.
    Proper indentation would show you straight away where that problem was.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: reading and writing string on notepad file

    Tolls highlighted a good point. Indent properly your code, then it is easy to read as well as easy to find all the typing mistakes (as you did in your code). As a programmer familiarize with those kind of habits. Those are valued a lot with the experience you gain.

    Regarding your code, note that how you make trouble yourself because of not indent properly. I have added some comments on it.


    Java Code:
                try { // Start the try block
                    print = new PrintWriter(new BufferedWriter(new FileWriter("squares.txt")));
    
                    for (int i = 1; i <= 10; i++) { // Start the for loop
                        z = Integer.parseInt(readLine());
                        int x = z * z;
                        print.println(z + " ");
    
                    } // End of for loop
     
                 catch (IOException iox) { // Start the catch block, without a relevant try block
                    System.out.println("Problem writing " + fileName);
    
                }

  4. #4
    janroz is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: reading and writing string on notepad file

    thanks you guys! yes sir and maam i will do it next time... well can you help me what is tokenizer all about?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: reading and writing string on notepad file

    If this is a new question (and it's not all that clear) then I'd recommend starting a new thread.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: reading and writing string on notepad file

    I agreed with Tolls. If you have a question something non-related to the original please start a new thread in relevant sub-forum.

    Do you really able to solve your original issue?

Similar Threads

  1. Writing and Reading from a file?
    By SilentCoder in forum New To Java
    Replies: 5
    Last Post: 06-10-2011, 03:12 AM
  2. File reading / writing
    By MattBSibley in forum New To Java
    Replies: 5
    Last Post: 04-19-2010, 05:20 AM
  3. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  4. Reading data from Micrsoft excel and writing to notepad
    By abhishek.jain in forum New To Java
    Replies: 4
    Last Post: 01-29-2009, 08:12 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •