Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code that gets user input and should initiate variables conditionally

package inout;

import java.io.InputStreamReader;
import java.io.*; 
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

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 a = reader.readLine();
      System.out.print("The correct answer is: " + a);

      //String a = null ;
      
      String b = "" ;
      String c = "" ;
      String d = "" ;

      
          if (a=="")
      {                
        b = "D" ;       
        c = "N" ;                       
        d = "S" ;       
      }                
      

      String n = reader.readLine();

      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word + "  which is "+ a + b + c + d + " and 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();
      }
    }
  }
}


if user enters " " for a then other variables b, c and d should change to D, N and S
but they dont

What I have tried:

changed code and searched java classes
Posted
Updated 13-May-18 11:22am

1 solution

if (a=="")

Incorrect.
Try:
if(a.compareTo("")==0)


Or

if(a.isEmpty()==true)
 
Share this answer
 
Comments
four systems 14-May-18 2:34am    
wow, :)

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