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..
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.
Re: reading and writing string on notepad file
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.
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.
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);
}
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?
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.
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?