Member Avatar for jeanfrg

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

When I run the program and choose option 4 or 5 nothing happens, it skips the rest of the loop and starts again.

#This is the main code that uses all the methods.

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 think that the problem is in this class.

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'

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.(:

some character are metacharacter in regex and has special meaning. If you want to match them you need to escape.
in your cas it's the "*" caractere

made this change please

temp = emailData.split("[B]\\[/B]*");

hope it helps.

Member Avatar for jeanfrg

It didn't work :/
I got option 4 working by post incrementing.

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;
    }

Is there some bug in this one?

public String writeEmail(String emailData){
        emailData = emailData + "`";
        System.out.print(": ");
        emailData = in.nextLine();
        return emailData;
    }

what error you get?

Member Avatar for jeanfrg

I get no error it just skips the rest of the loop. (does nothing)
There are no syntax errors.

witch loop?

Member Avatar for jeanfrg

This loop.
When you choose option 5 (case 5), the rest of the loop is skipped and starts from the beginning.

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");
        }
    }
}

Can you please tell what that red instruction means:

public String writeEmail(String emailData){
        emailData = emailData + "`";
        System.out.print(": ");
        [B]emailData = in.nextLine();[/B]
        return emailData;
    }
Member Avatar for jeanfrg

It reads the input. The string input.

I now but in the context, I mean it's logic!

Member Avatar for jeanfrg

It allows you to write an email. Obviously it doesn't actually write an email it just writes to a string.

before that nstruction the value helt by emailData is :

John1987@gmail.com*This a test email.*

but after the return value is empty!!

Member Avatar for jeanfrg

I'm sorry I don't understand what you are trying to say. The return value has the new "email" + the old one too. And then it splits them.

No! it return an empty String!

Member Avatar for jeanfrg

why? I for the least I entered "'"

check the javaDoc's nextLine before going with tread please!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.