Results 1 to 7 of 7

Thread: Simple java program

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    3

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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);}
     }
    }

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    3

    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?

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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"))

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    3
    I had change If (sel.equal("1")), but compiler could not resolve the symbol
    Last edited by bonboncat; Jul 24th, 2003 at 04:35 AM.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Java is case sensitive, it's if, not If.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width