Jump to content

Need a lil bit of help Strings / Ints from line of text!


Recommended Posts

Posted

I'm struggling with my onMessage(), this is what I wont - e.g.

when it comes up in chat box -

 

your ring of dueling has 6 charges remaining.

 

how do I go about storing only certain parts of that text e.g.

 

ringName = ""Ring of dueling"; chargesLeft = 6.

 

thanks in advance to anyone that can help me :)

 

Posted
public void onMessage(Message message){
    String msg = message.getMessage(); //gets the raw message from the chatbox
    //perform trimming
}

 

yes I have that part, its mainly the trimming part I'm struggling with, is there anywhere I can read up? can some1 pnt me in the right direction? I'm not really sure what I need to read up on when it comes to trimming text :p

 

Posted

Why not do everything the hard way.

 private int ringUsesLeft(){
        Item inventoryItem = inventory.getItem(new Filter<Item>() {
            @Override
            public boolean match(Item item) {
                return item != null && item.getName().contains("Ring of dueling");
            }
        });
        Item wornItem = equipment.getItem(new Filter<Item>() {
            @Override
            public boolean match(Item item) {
                return item != null && item.getName().contains("Ring of dueling");
            }
        });
        if(inventoryItem != null){
            String toRip = inventoryItem.getName();
            String butchered = toRip.replace("Ring of duelling(", "").replace(")", "").replace(" ", "");
            if(butchered != null) {
                return Integer.valueOf(butchered);
            }else{
                log("We Fucked Up!");
            }            
        }else if(wornItem != null){
            String toRip = wornItem.getName();
            String butchered = toRip.replace("Ring of duelling(", "").replace(")", "").replace(" ", "");
            if(butchered != null) {
                return Integer.valueOf(butchered);
            }else{
                log("We Fucked Up!");
            }
        }
        return 0;
    } 

i'm like 99.99% sure I don't know the exact name spacing so it'll need tweaking 

Posted

get the substring of the string. then convert the string into an int

 

http://www.tutorialspoint.com/java/java_string_substring.htm

 

thanks, gunna look into this right away, much appreciated, il let you no how I get on, thanks again :)

Why not do everything the hard way.

 private int ringUsesLeft(){
        Item inventoryItem = inventory.getItem(new Filter<Item>() {
            @Override
            public boolean match(Item item) {
                return item != null && item.getName().contains("Ring of dueling");
            }
        });
        Item wornItem = equipment.getItem(new Filter<Item>() {
            @Override
            public boolean match(Item item) {
                return item != null && item.getName().contains("Ring of dueling");
            }
        });
        if(inventoryItem != null){
            String toRip = inventoryItem.getName();
            String butchered = toRip.replace("Ring of duelling(", "").replace(")", "").replace(" ", "");
            if(butchered != null) {
                return Integer.valueOf(butchered);
            }else{
                log("We Fucked Up!");
            }            
        }else if(wornItem != null){
            String toRip = wornItem.getName();
            String butchered = toRip.replace("Ring of duelling(", "").replace(")", "").replace(" ", "");
            if(butchered != null) {
                return Integer.valueOf(butchered);
            }else{
                log("We Fucked Up!");
            }
        }
        return 0;
    } 

i'm like 99.99% sure I don't know the exact name spacing so it'll need tweaking 

 

its not just about teleports, I wonted it for poison, antifire, kills remaining on slayer task ect... but thanks for the post appreciated, I just need to use the server messages for most of the things needed to be stored :)

 

Posted (edited)

thanks, gunna look into this right away, much appreciated, il let you no how I get on, thanks again smile.png

 

its not just about teleports, I wonted it for poison, antifire, kills remaining on slayer task ect... but thanks for the post appreciated, I just need to use the server messages for most of the things needed to be stored smile.png

 

 @Override
    public void onMessage(Message m) throws InterruptedException {
        super.onMessage(m);
        /*message you want : our ring of dueling has 6 charges remaining. , cut it back to the number
        so you could do : our ring of dueling has 
        or look for both halves exlcuding the number: "our ring of dueling has " , " charges remaining".
        but the first should work anyway
        */
        String theMessage = "our ring of dueling has ";
        if(m.getMessage().contains(theMessage)){
           String butcherMessage = m.getMessage().replace(theMessage, "").replace(" charges remaining.", "");
            int result = Integer.valueOf(butcherMessage);
            //use result here
        }
    }

Edited by Isolate
Posted

You could also split the string, which would be easier.

if you split the string it will be harder for him to find what he wants.

 @Override
    public void onMessage(Message m) throws InterruptedException {
        super.onMessage(m);
        /*message you want : our ring of dueling has 6 charges remaining. , cut it back to the number
        so you could do : our ring of dueling has 
        or look for both halves exlcuding the number: "our ring of dueling has " , " charges remaining".
        but the first should work anyway
        */
        String theMessage = "our ring of dueling has ";
        if(m.getMessage().contains(theMessage)){
           String butcherMessage = m.getMessage().replace(theMessage, "").replace(" charges remaining.", "");
            int result = Integer.valueOf(butcherMessage);
            //use result here
        }
    }

@op http://www.tutorialspoint.com/java/java_string_replace.htm

Posted

use regex

 

String str = getMessage().replaceAll("\\D+", "");

Integer.parseInt(str); 

 

iv accually just been looking at regex, iis there a method to only read numbers, then can I make that into and int for counting with?

basicly like this =

String text = "Hello, my name is bob, and I'm 37";

 

int age = 37;

use regex

 

String str = getMessage().replaceAll("\\D+", "");

Integer.parseInt(str); 

 

Thanks for this :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...