-
Simple java program
I'm beginner in java . now i try to write a simple program to keep the friends particulars and relative particulars.
on the screen will display the following information:
1. friends information
2. relative information
Please enter your selection :
Now, my problems are how do write the code to read the selection from the user. for example user key in "1" then the friend information screen will be appear.
second, how to clear screen in dos.
-
There is no proper screen clearing. Simply output a lot of (~24) newlines (standard console height is 24 lines).
For all other questions:
http://java.sun.com/
has great tutorials.
-
Any console programs can get information from the user in one of two ways. Either by accessing the information stored in the args String[] array or by using Streams. The first block of code just prints out the arguements passed in on the command line. The second block of code uses a combination of different streams to obatin the user input.
Code:
public class C{
public static void main(String[] args){
for(int i = 0; i < args.length; i++){
System.out.println(args[i]);
}
}
}
Code:
import java.io.*;
public class C{
public static void main(String[] args){
try{
System.out.println("Enter data to be echoed");
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String s = buff.readLine();
System.out.println(s);
}catch(IOException e){System.err.println(e);}
}
}
-
:)
Thanks Dilenger4!
now i continue write some code and some errors come out:
mport java.io.*;
public class Assignment {
public static void main (String args []) {
try{
System.out.println(" Welcome to the Friendship CLUB\n\n");
System.out.println("1. Friend\n");
System.out.println("2. Relative\n");
System.out.println(" Please Enter your Selection:" );
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String sel = buff.readLine();
System.out.println(sel);
}catch(IOException e){System.err.println(e);}
If (sel = 1)
{
try{
System.out.println(" Friend Information\n\n");
System.out.println(" Friend Name : ");
System.out.println(" Friend address: ");
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String name = buff.readLine(); String add = buff.readLine();
System.out.println(name);
System.out.println(add);
}catch(IOException e){System.err.println(e);}
} else
{System.out.println(" Sale\n\n");}
}
My Questions are:
1) Why String sel does not accept 1?
2) am I right in my code?
-
Taking a quick look at your code you are using the assignment opperator in your if() statement. It should look like this if(sel.equals("1"))
-
:) I had change If (sel.equal("1")), but compiler could not resolve the symbol
-
Java is case sensitive, it's if, not If.