Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

Thread: help i wasn't taught this yet

  1. #1
    chins4tw is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default help i wasn't taught this yet

    i am currently doing my culmonating and if anyone can can direct me to how to finish my phone book i will be grateful i need to know how to save already entered phone numbers

    package personal_organizer;
    import java.io.*;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.Scanner;
    public class Personal_Organizer{
    public static void main(String[] args){
    Scanner input= new Scanner(System.in);
    String Number,name;

    System.out.println("Phone book of random numbers.");

    do{
    System.out.println("You may stop this by making the name 999."
    + "You must finish this entry form first but it will not count");
    System.out.print("Please enter a new Phone number: ");Number= input.nextLine();
    System.out.print("Please enter a name for the new phone number: ");name= input.nextLine().toLowerCase();

    if(name.charAt(0)=='a'|| name.charAt(0)=='b'||name.charAt(0)=='c'
    ||name.charAt(0)=='d'||name.charAt(0)=='e'||name.c harAt(0)=='f'){System.out.println("A-F");
    System.out.println(name);System.out.println(Number );}

    if(name.charAt(0)=='g'|| name.charAt(0)=='h'||name.charAt(0)=='i'
    ||name.charAt(0)=='j'||name.charAt(0)=='k'){System .out.println("G-K");
    System.out.println(name);System.out.println(Number );}

    if(name.charAt(0)=='l'|| name.charAt(0)=='m'||name.charAt(0)=='n'
    ||name.charAt(0)=='o'||name.charAt(0)=='p'){System .out.println("L-P");
    System.out.println(name);System.out.println(Number );}

    if(name.charAt(0)=='q'|| name.charAt(0)=='r'||name.charAt(0)=='s'
    ||name.charAt(0)=='t'||name.charAt(0)=='u'){System .out.println("Q-U");
    System.out.println(name);System.out.println(Number );}

    if(name.charAt(0)=='v'|| name.charAt(0)=='w'||name.charAt(0)=='x'
    ||name.charAt(0)=='y'||name.charAt(0)=='z'){System .out.println("V-Z");
    System.out.println(name);System.out.println(Number );}
    }while(!name.equals(999));




    }
    }


    is my code right now

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: help i wasn't taught this yet

    Hi
    Java Code:
    // Here we create a TreeMap where the keys and values are of type String (maps hold key : value pairs)
    TreeMap<String, String> phoneBook = new TreeMap<String, String>();
    // This is how a value is added in the map
    phoneBook.put(/*key*/ name, /*number*/ number);
    As a side note, have a look at switch/case clause, this is how your code will look like with switch/case:
    Java Code:
    switch(name.charAt(0)) {
    	case 'a':
    	case 'b':
    	case 'c':
    	case 'd':
    	case 'e':
    	case 'f': {System .out.println("A-F"); System.out.println(name);System.out.println(number); break;}
    	case 'g':
    	case 'h':
    	case 'i':
    	case 'j':
    	case 'k': {System .out.println("G-K"); System.out.println(name);System.out.println(number); break;}
    	case 'l':
    	case 'm':
    	case 'n':
    	case 'o':
    	case 'p': {System .out.println("L-P"); System.out.println(name);System.out.println(number); break;}
    	case 'q':
    	case 'r':
    	case 's':
    	case 't':
    	case 'u': {System .out.println("Q-U"); System.out.println(name);System.out.println(number); break;}
    	case 'v':
    	case 'w':
    	case 'x':
    	case 'y':
    	case 'z': {System .out.println("V-Z"); System.out.println(name);System.out.println(number); break;}
    	default: { System.out.println("First symbol is not from the alphabet."); break;}
    }
    BR,
    Kalin
    "One of the greatest ways to learn something is trying to teach it to someone else."

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: help i wasn't taught this yet

    In my opinion, a switch clause here is rather unwieldy. Since characters can be compared to each other why not just use if statements.

    Java Code:
    char c = name.charAt(0);
    if (c < 'a' || c > 'z') {
        // error code here
    } else if (c <= 'f') {
       // handle a thru f
    } else if (c <= 'k') {
       // handle g thru k
    } else if (c <= 'p' ) {
       // ...
    } else if (c <= 'u') {
       // ...
    } else  {
       // all that's left is v thru z
    }
    This could be reduced (and perhaps optimized) by using other techniques. For example, by using an interface coupled with a hash table.

    Now to get to your question. To save them you need to determine what the output format is. Easiest way is to probably store them in
    a single text file, separated by some delimiter. The delimiter needs to be something that would not be in additional information you would want to incorporate into your phone book in the future (like part of a postal address or email address). Check out the PrintWriter class where you can use similar methods that System.out uses to print to files.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: help i wasn't taught this yet

    Java Code:
    char c = name.charAt(0);
    if (c < 'a' || c > 'z') {
        // error code here
    } else if (c <= 'f') {
       // handle a thru f
    } else if (c <= 'k') {
       // handle g thru k
    } else if (c <= 'p' ) {
       // ...
    } else if (c <= 'u') {
       // ...
    } else  {
       // all that's left is v thru z
    }
    I wasn't sure about that :) Also worth pointing out that switch/case is really slow(but yet more readable than the long expressions).

    BR,
    Kalin
    "One of the greatest ways to learn something is trying to teach it to someone else."

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: help i wasn't taught this yet

    Quote Originally Posted by kalata View Post
    I wasn't sure about that :) Also worth pointing out that switch/case is really slow(but yet more readable than the long expressions).
    Care to explain why a switch statement would be slow?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: help i wasn't taught this yet

    Care to explain why a switch statement would be slow?
    Well, after a little google-ing I didn't find facts to back up my post, only such putting it "2 feet below". I really can't remember in which class at university this was mentioned and the backing up of the statement..

    Conclusion: I stand corrected. Sorry for posting info without back up with certain knowledge or facts. :|

    Cheers,
    Kalin
    Last edited by kalata; 06-07-2013 at 10:05 PM.
    "One of the greatest ways to learn something is trying to teach it to someone else."

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: help i wasn't taught this yet

    aamof, the switch statement is extremely fast; it comes in two flavours: a search table and a jump table; the first table stores pairs <value, target> where the table is ordered by the value; a binary search finds the target address where to jump to in O(log(n)); the second version is even faster: the value is used as an index in the table where the target addresses are stored; this only works if the table is very dense; your example (see above) is such a very dense switch table.

    The flip side is that it only works for ints (a little trick also makes it work for Strings, but additional code is needed and the hash value for the String values needs to be calculated by the compiler).

    kind regards,

    Jos
    kalata likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: help i wasn't taught this yet

    Interesting. Since this is an implementation detail it wouldn't be in the JLS. So from what source did you glean the information?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: help i wasn't taught this yet

    Quote Originally Posted by jim829 View Post
    Interesting. Since this is an implementation detail it wouldn't be in the JLS. So from what source did you glean the information?
    It's in the JVM machine specification; another fine document ;-) It's in chapter 7.10

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: help i wasn't taught this yet

    Actually chapter 3.10. Although Oracle's implementation of what byte codes a switch statement compiles to may be very efficient (or even the most efficient) I assume other venders are free to do it differently.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: help i wasn't taught this yet

    Quote Originally Posted by jim829 View Post
    Actually chapter 3.10. Although Oracle's implementation of what byte codes a switch statement compiles to may be very efficient (or even the most efficient) I assume other venders are free to do it differently.
    My copy of the specification is the second edition; maybe they mixed up the chapters again in a further edition; nevertheless, Sun's/Oracle's implementation has two special instructions for the implementation of a switch statement; my guess is that the actual lookup or search in those tables is done in machine code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 12
    Last Post: 05-10-2012, 08:11 PM
  2. Class Wasn't Found in Project
    By BrooksyFC in forum NetBeans
    Replies: 14
    Last Post: 05-03-2012, 10:35 AM
  3. I taught Java 1 ...
    By livingdog in forum Introductions
    Replies: 4
    Last Post: 01-15-2010, 10:59 PM

Tags for this Thread

Posting Permissions

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