Results 1 to 8 of 8
  1. #1
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default help me wif tis pls...

    import java.util.StringTokenizer;

    import java.util.*;;

    public class Question1{
    public static void main(String[]srgs){
    int x=0;


    Scanner scan=new Scanner(System.in);

    System.out.println("Enter your name:");
    String s=scan.nextLine();

    StringTokenizer tokenizer = new StringTokenizer(s);



    System.out.println("Enter your address:");
    String t=scan.nextLine();


    while (tokenizer.hasMoreTokens())

    System.out.print(tokenizer.nextToken().charAt(0));
    System.out.print(" ");


    for(int i=0;i<t.length();i++){


    if(Character.isDigit(t.charAt(i)))
    System.out.print(t.charAt(i));

    }
    System.out.println();
    System.out.println("Do you want to continue?");
    String answer=scan.nextLine();




    while(answer != "no"){

    System.out.println("Enter your name:");
    s=scan.nextLine();




    System.out.println("Enter your address:");
    t=scan.nextLine();

    while (tokenizer.hasMoreTokens())

    System.out.print(tokenizer.nextToken().charAt(0));

    for(int i=0;i<t.length();i++){


    if(Character.isDigit(t.charAt(i)))
    System.out.print(t.charAt(i));

    }
    System.out.println("Do you want to continue?");
    answer=scan.nextLine();



    }
    }
    }


    my tokenizer seems din work during the 1st loop...
    ex: input is name and address(Anngie Tan,103 Jalan weng)
    i want it to print the each initial of name and the number frm address,( AT 103),but it works only on the initial reading

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    English isn't my first language. Please translate this, and I will try to help you. Also, please avoid unusual or non-standard abbreviations for this reason:
    seems din work

  3. #3
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    sorry...this is the question..

    Write an application that prompts a user for a full name and street address and your program will construct an ID automatically from the user’s initials and numeric part of the address. For example, the user Mohd Ali Ahmad who lives at 13 Kenari Road would have an ID of MAA13, whereas user Jenifer Lopez who lives at 3101 Crystal would have an ID of JL3101.

    For example,

    Name: Mohd Ali Ahmad
    Address: 13 Kenari Road, Bird Street, Malaysia.

    ID: MAA13

    Name: Jenifer Lopez
    Address: 3101 Crystal, Clear Street, Malaysia.

    ID: JL3101

    and, etc.

    For every record filled, ask if your user wish to continue on filling in more records using ‘Yes’ or ‘No’ sentinel value. If ‘No’ is selected it should terminate or end your application.



    but my problem is i cannot terminate the looping although i type "no"
    and my first looping doest printout the initial of name...just number only...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thank you. A suggestion:

    Don't compare Strings with == or !=:
    Java Code:
    while (answer != "no")
    Instead use equals, or equalsIgnoreCase, or in this situation
    Java Code:
    while (!answer.equalsIgnoreCase("no"))
    A String is an object, and when you use == or !=, you are checking if one String is the same object or a different object from another String. This is not what you want to know. You want to know if one String contains the same content (letters) as another String, hence the use of equals(...)

  5. #5
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    thank you for your help...now i can terminate the loop already but the second id printed out doest show the initial of the name...just have number only...how can i do?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem is that there is needless repetition in your program. You have two blocks of code that are doing almost exactly the same thing. The first block gets called always and puts out the correct output. The second gets called within a loop only if the user desires. You say that the second time the user gives input the output is wrong. This should be setting off bells in your head. "hmmm, perhaps the two blocks are different?"

    Answer: they are.

    So: find the differences and fix the second block. Better yet, use a do/while loop and have only one block of code doing everything. Also, always place all logical blocks in curly braces. This means all while blocks, for blocks, if blocks, even and especially if only one line long. You'll thank me later.

  7. #7
    sinyi88 is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    i got the correct output already...thanks a lot...you are too expert

  8. #8
    kaemonsaionji is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

Posting Permissions

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