Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code that assigns some value to variable y and prints on console, would want the code to reassign value and print

public class Ifelse
{
    public static void main(String[] args) throws IOException
    {     
     String d = null ;      
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("A:" ); 
     String y = reader.readLine();
     System.out.print("You entered: " + y);
     String Word = reader.readLine();   
     System.out.print("A:" );
     String y = reader.readLine(); 
     System.out.print("NowAis: " + z);
    }
}


the code says variable y is already defined, would want to reassign value to y

regards

What I have tried:

changed code and searched java classes
Posted
Updated 13-May-18 5:52am
Comments
Richard MacCutchan 13-May-18 12:08pm    
This is much the same as your previous question where you seem to be using random variables without really understanding their lifetime. I would strongly suggest you go to The Java™ Tutorials[^] and learn the basics. And using meaningful names for your variables instead of a,y and z will help to make things clearer.
four systems 13-May-18 12:22pm    
t

1 solution

And so it is. You can't create two variables with the same name in the same scope: the system wouldn't know which one you meant to use!
public static void main(String[] args) throws IOException
{
 String d = null ;
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("A:" );
 String y = reader.readLine();
 System.out.print("You entered: " + y);
 String Word = reader.readLine();
 System.out.print("A:" );
 y = reader.readLine();
 System.out.print("NowAis: " + y);
}
Should fix it.
 
Share this answer
 
v2
Comments
four systems 13-May-18 12:27pm    
Y does changes and prints now,:)
CPallini 13-May-18 12:31pm    
5.

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