Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default help to understand the ? mark in this code

    can somebody help me to understand this line of code
    s = args.length == 1 ? args[0] : "";

    in the following program :

    /* <assign> => <id> = <expr> ;
    * <id> => a | b | c
    * <expr> => 0 | ... | 9 | (<assign>)
    */



    public class Main {

    public static void main(String[] args) {

    s = args.length == 1 ? args[0] : "";

    if (assign() && i == s.length()) {
    System.out.println("The string \"" + s + "\" is in the language.");
    }
    else {
    System.out.println("The string \"" + s + "\" is not in the language.");
    }
    }

    private static boolean assign() {

    if (id()) {
    if (i < s.length() && s.charAt(i) == '=') {
    ++i;
    if (expr()) {
    if (i < s.length() && s.charAt(i) == ';') {
    ++i;
    return true;
    }
    }
    }
    }

    return false;
    }

    private static boolean expr() {

    if (i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
    ++i;
    return true;
    }
    else if (i < s.length() && s.charAt(i) == '(') {
    ++i;
    if (assign()) {
    if (i < s.length() && s.charAt(i) == ')') {
    ++i;
    return true;
    }
    }
    }

    return false;
    }

    private static boolean id() {

    if (i < s.length() && s.charAt(i) >= 'a' && s.charAt(i) <= 'c') {
    ++i;
    return true;
    }

    return false;
    }

    private static String s;
    private static int i;
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  3. #3
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 08:18 PM
  2. how to mark as Correct or Wrong ?!
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 05-21-2009, 11:52 AM
  3. Mark thread RESOLVED.
    By Eranga in forum Suggestions & Feedback
    Replies: 45
    Last Post: 04-02-2008, 11:34 AM
  4. BufferedReader .mark(int readAheadLimit)
    By ShoeNinja in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 11:58 PM

Posting Permissions

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