Click here to Skip to main content
13,704,865 members
Rate this:
 
Please Sign up or sign in to vote.
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 13-May-18 7:19am
Updated 13-May-18 11:22am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

if (a=="")

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


Or

if(a.isEmpty()==true)
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 13 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100