Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code that writes to file , the variable a which is type string is initially
initialized to "not" should change to " "

public class BufferedReaderFromUserAnd
{
    private static final String FILENAME = "F:/Android.txt";   

    public static void main(String args[]) throws IOException
    {
    BufferedWriter bw = null;
    FileWriter fw = null; 
    
    try
    {
    fw = new FileWriter(FILENAME);
    bw = new BufferedWriter(fw);        
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));  
    System.out.print("Word: "); 
    String Word = reader.readLine();     
    System.out.print("Y: ");    
    String y = reader.readLine();
    System.out.print("The correct answer is: " + y); 
    
    String a = "not" ;
    String b = "not" ;
    String c = "not" ;           
    String d = "not" ;  
    
    if (y==a)
    {
        System.out.print("A: ");
        a = reader.readLine();
        a ="";
    }        
    
    String n = reader.readLine();               
           
    bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word + "  which is "+ a +" the correct answer' />(a) "  + Word + "<br/>");   
    }   
    catch (IOException e) {
    e.printStackTrace();
    } finally 
    {
    try {
	if (bw != null)
	bw.close();
	if (fw != null)
	fw.close();
	} catch (IOException ex) {
	ex.printStackTrace();
	}
    }
    
    
    }   
}


<input type='radio' name='rbnNumber' value='You selected (a) cs which is not the correct answer' />(a) cs<br/>

after variable a is " " the new string should read

<input type='radio' name='rbnNumber' value='You selected (a) cs which is the correct answer' />(a) cs<br/>

What I have tried:

changed code and searched java classes
Posted
Updated 11-May-18 2:00am
v2
Comments
Richard MacCutchan 11-May-18 9:33am    
What did you enter at each of the prompts?
four systems 11-May-18 11:50am    
cause its a larger code, this is abstract. What i entered at prompts Word is
Android; which !does write to file, what should write to file is the prompt Y which i entered a, now that should write to "" + a + "" but it doesnt change. another way when user enters from console then a changes but cause want it automated thats why did this way, its straightforward variable a from prompt and writes to + a + should write

1 solution

The try/catch structures prevent you from seeing any problem in code.
The only ways to know what is doing your code is by removing the try/catch or by using a debugger.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Java
public class BufferedReaderFromUserAnd
{
  private static final String FILENAME = "F:/Android.txt";

  public static void main(String args[]) throws IOException
  {
    BufferedWriter bw = null;
    FileWriter fw = null;

    try
    {
      fw = new FileWriter(FILENAME);
      bw = new BufferedWriter(fw);

      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Word: ");
      String Word = reader.readLine();
      System.out.print("Y: ");
      String y = reader.readLine();
      System.out.print("The correct answer is: " + y);

      String a = "not" ;
      String b = "not" ;
      String c = "not" ;
      String d = "not" ;

      if (y==a)
      {
        System.out.print("A: ");
        a = reader.readLine();
        a ="";
      }

      String n = reader.readLine();

      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word + "  which is "+ a +" the correct answer' />(a) "  + Word + "<br/>");
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally
    {
      try {
        if (bw != null)
          bw.close();
        if (fw != null)
          fw.close();
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
Richard MacCutchan 11-May-18 9:34am    
The use of try/catch does not affect the result.
Patrice T 11-May-18 9:58am    
try/catch prevent from getting error messages
Richard MacCutchan 11-May-18 11:21am    
Both catch blocks catch IOException and print a stack trace if it occurs. And since the rest of the code is unlikely to throw any exception it does not make any difference.
four systems 11-May-18 12:07pm    
this code also does not changes variable a's value, a is initially "not" but then when value is entered at prompt y a should change to " " and the sentence should read "You selected a which is the correct answer" , sounds straightforward but then it !is
four systems 11-May-18 12:27pm    
the bit that should change a is what !does change a

if (y==a)
{
System.out.print("A: ");
a = reader.readLine();
a ="";
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900