Results 1 to 9 of 9
  1. #1
    jeanfrg is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Need help with Assignment. [String splitting] [Full code provided]]

    Hi, I need help with an Assignment that requires me to continue(inheritance) on an existing assignment that I previously did.

    When I compile them there are no syntax errors, but when I try to run the code it says "PatternSyntaxExeption: null(in java.util.regex.Pattern)". Do I need to use regex?

    #This is the main code that uses all the methods.
    XML Code:
    import java.util.Random;
    import java.util.Scanner;
    
    class pdaSIM{
        public static void main(String args[]){
            pdaCMD pda = new pdaCMD();
            Random rand = new Random();
            Scanner in = new Scanner(System.in);
            
            double credit = 0;
            String battery_stat="[#][#][#][][]";
            String emailData = "John1987@gmail.com*This a test email.";
            
            credit = 2+rand.nextInt(10);
            //use power procedudure
            pda.power = true;
            //start of main code
            //loops until power is false
            while (pda.power == true){
                //split emailData string
                emailData = pda.splitEmail(emailData);
                //displays layout
                pda.showCredit(credit);
                System.out.print("\t");
                pda.batteryStatus(battery_stat);
                
                //displays menu
                pda.menu();
                //checks option chosen
                switch (pda.menuCh) {
                    case 1: credit = pda.Call(credit); break;
                    case 2: credit = pda.topup(credit); break;
                    case 3: battery_stat = pda.recharge(battery_stat); break;
                    case 4: emailData = pda.readEmail(emailData); break;
                    case 5: emailData = pda.writeEmail(emailData); break;
                    case 6: //switch pda off
                            pda.power(); break;
                    default: System.out.print("Invalid choice.\n"); break;
                }
                
                System.out.print("\n");
            }
        }
    }
    #I get the error in this class.
    XML Code:
    import java.util.Scanner;
    public class pdaCMD extends mobileCMD{
        Scanner in = new Scanner(System.in);
        
    String temp[];
        
        public String splitEmail(String emailData){
            temp = emailData.split("*");
            return emailData;
        }
        
        public String readEmail(String emailData){
        for (int i=0; i<temp.length; i++){
            if (temp[i].endsWith(".com")){
                  System.out.print("\nFrom: " +temp[i]+ "\n");
                  System.out.print("\t" +temp[i+1]+ "\n");
            } else {
                i++;
            }
        }
        return emailData;
        }
        
        public String writeEmail(String emailData){
            emailData = emailData + "*"; 
            System.out.print(": ");
            emailData = in.nextLine();
            return emailData;
        }
        
        public void menu(){
            System.out.print("\n\n1. Call\n2. Topup\n3. Recharge\n4. Read Email\n5. Write email\n6. Power\t");
            menuCh = in.nextInt();
        }
    }
    #This class extends 'pdaCMD'
    XML Code:
    import java.util.Scanner;
    public class mobileCMD{
        Scanner in = new Scanner(System.in);
    
    boolean power = false;
    int menuCh;
        
        public void power(){
            while (power == false){
                System.out.print("\tPower on.\n");
                power = true;
                break;
            }
            
            while (power == true){
                System.out.print("\n\tPower off.");
                power = false;
                break;
            }
        }
        
        public double Call(double credit){   
            if (credit <= 0){
                System.out.print("Please topup credit.\n");
            }
            
            if (credit > 0){
                credit = credit-0.5;
            }
            return credit;
        }
        
        public void showCredit(double credit){
            System.out.print(credit+ "euros");
        }
        
        public double topup(double credit){
            credit = credit+5;
            return credit;
        }
        
        public void batteryStatus(String battery_stat){
            System.out.print(battery_stat);
        }
        
        public String recharge(String battery_stat){
            if (battery_stat == "[#][#][#][#][#]"){
                System.out.print("Battery charged.\nPlease unplug charger to save energy.\n");
            }
            
            battery_stat = "[#][#][#][#][#]";
            return battery_stat;
        }
        
        public void menu(){
            System.out.print("\n\n1. Call\n2. Topup\n3. Recharge\n4. Power\t");
            menuCh = in.nextInt();
        }
        
    }
    Please help.
    Anyone can use the code, if used a little indication that this was made by me (jean farrugia) would be much appreciated.(:

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

    Default

    You can't split a String on the "*" character because the split method uses regular expressions for its split condition and the * character is a special character for the regular expression engine. You can escape it however to suppress its special meaning: split on "\\*" instead (two backslashes).

    kind regards,

    Jos

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

    Default

    Which line causes the error? Is it the one where you try to split a String with "*"? When you use split are are in fact using regex, so you 'll have to escape that "*" since "*" has special meaning in the regex world. Try:

    Java Code:
    temp = emailData.split("\\*");

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

    Default

    D'oh! Curse you Jos! Beaten again!

  5. #5
    jeanfrg is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    OK thank you for your help :D
    No syntax errors.
    I used "|"... But now... The code still doesn't work... when you choose option 4 or 5 nothing happens...

    Any Suggestions?
    Thanks again.

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

    Default

    Quote Originally Posted by jeanfrg View Post
    OK thank you for your help :D
    No syntax errors.
    I used "|"... But now... The code still doesn't work... when you choose option 4 or 5 nothing happens...

    Any Suggestions?
    Thanks again.
    The vertical bar character "|" is also a special character for regular expressions. Read the API documentation for the Pattern class and read the syntax description for regular expressions.

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by Fubarable View Post
    D'oh! Curse you Jos! Beaten again!
    neener, neener, neener!

    kindest regards,

    Jos ;-)

  8. #8
    jeanfrg is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Ok now I changed it to "_"(:
    what about my other problem? can you help me with that?

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

    Default

    Quote Originally Posted by jeanfrg View Post
    Ok now I changed it to "_"(:
    what about my other problem? can you help me with that?
    What other problem? Don't write "it doesn't work" or "nothing happens". You can stick in System.out.println( ... ) statements at strategic places to check what your code is doing.

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 05-06-2010, 07:10 AM
  2. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  3. String Splitting
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 07:17 AM
  4. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 11:51 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 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
  •