Jump to content

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


moler

Recommended Posts

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

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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