Casting an int to a string to work indexOf method

I'm trying to see what interger value occurs in a specific token.
For examplethe token: "6-" the integer 6 occurs.
But I have to pass the indexOf method a string.
if (tokenCountdown == 0)
     while(gotGoals != 1)
          String(varGoal);
          if (tokenStr.indexOf(varGoal) = -1)
               goalsScored = (int)(varGoal);
               gotGoals = 1;
               varGoal++;
     }Any ideas on how to get around this?

I have tokenised a text file and the specific token
can contain a string of form XX-
X being interger values.
What I want to do is extract whatever integer value is
XX and store it in a variable.
Again, not clear. In "XX-X", are "XX" and "X" both integers? As in "12-3" and you want to extract "12" and "3"?
Is the first int always 2 digits and the second always one? In short, what, precisely, are the invariants in terms of lenght, position, etc.?
You reckon I cant use indexOf? What will you do indexOf on? If it's always "somenumber-someothernumber" then you can use indeOf("-") to find the split point.
What method can I use?
subString?You could use that in combination with indexOf("-"), yes.
Or you could use StringTokenizer, or String.split, or regex if it's actually more complex than I've been assuming. Assuming it's always "num1-num2", but num1 and num2 are not fixed length (e.g. couuld be 1 or 2 digits) StingTokenizer is probably the simplest.
If num1 and num2 are a fixed number of digits, you could just use substring, since you know exactly which positions you want.
Ultimately, which is best depends on your constraints. What's variable, what's fixed, how much flexibility you need.

Similar Messages

  • Warning [deprecation] setStatus(int,javax.lang.String)

    warning [deprecation] setStatus(int,javax.lang.String) in HttpServletResponseWrapper
    can I find a jee.jar to remplace j2ee.jar to take the resolution of this problem

    The resolution of this problem isn't to fiddle about with .jar files, but to change the code to call the method now recommended instead of the deprecated method. Or just ignore the warning - the code will still work.
    BTW surely it's java.lang.String?

  • Converting an int to a string

    i want to know how to convert an int to a string .
    I have tried toString() but it says can't dereference an int.
    any ideas ????
    thanks

    What I mean by the object being null is, say for example, you have the following method:
       public String combine(Object o1, Object o2){
          return o1.toString() + o2.toString();
       }This method will throw a NullPointerException if either or both o1 and/or o2 are null. If you use it like so:
       public String combine(Object o1, Object o2){
          return String.valueOf(o1) + String.valueOf(o2);
       }This will always work. (I know someone out there would say that this would return a String like "nullnull" if both are null, and so forth and so on, but hey, you get my drift.) I'm also not saying the you can't not check if either o1 or o2 is null before proceeding, so the following also works:
       public String combine(Object o1, Object o2){
          String s = null;    // I'm using this instead of StringBuffer for
                              // simplicity's sake so don't get this wrong
          if (o1 != null){
              s = o1.toString();
          if (o2 != null){
              s += o2.toString();
          return s;
       }As you can see, there's no right or wrong way in programming as long as you achieve the result. The only thing that would matter is how clean your code is, how efficient your code is, and how maintainable your code is.

  • How to cast an int into an Object?

    If I want to push the int called result (e.g. result = 3 + 4) on my Stack, it doesn't work, because push asks for an Object.
    So how can I cast my int into an Object?
    Thanks for help!
    trampoline

    All the primitive types have wrapper classes. In this case use the following:
    int result = 3+4;
    Integer iResult = new Integer(result);
    Now push iResult onto your stack.

  • How can I convert an int to a string?

    Hi
    How can I convert an int to a string?
    /ad87geao

    Here is some the code:
    public class GUI
        extends Applet {
      public GUI() { 
        lastValue = 5;
        String temp = Integer.toString(lastValue);
        System.out.println(temp);
        showText(temp);
      private void showText(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            tArea2.setText(text + "\n");
    }

  • How to convert an int to a String

    Hi, just wanting to know is anyone could help me with convertin a primitive int into a String, currently I have
    String mod = Integer.valueOf(n);
    but with this i keep getting an error which says, - cannot resolve symbol - method valueOf(int)
    Can anyone please help me. thanks.

    it should be
    String mod = Integer.toString(n);

  • How do I convert an int to a String?

    How do I convert an int to a String?

    You can also use any of these methods if you need to get more complicated:
    Integer.toString(int i)
    Integer.toBinaryString(int i)
    Integer.toHexString(int i)
    Integer.toOctalString(int i)
    Integer.toString(int i, int radix)

  • Number to fractional string not working correctly

    I am measuring some parameters from oscillsocpe. i need to write these values to text file for which i am using write to spreadsheet, but what is happening is it is always writing zero value to file, on debugging i found that while converting from number to string it is always writng zero to string indicator, then separately i used that number to fractional string indicator and it is always showing zero in string output , i dont why it is happening.
    Attached below is the code.
    Solved!
    Go to Solution.
    Attachments:
    Time meas sub.vi ‏31 KB

    Ranjeet Singh and Norbert_B
    Thanks for ther reply
    Ranjeet singh,
    number to fractional string is working correctly, there is some other problem, because inly when i am executing this code converting this rise time value to string then only it is showing zero, otherwise for any random number if i am checking this number to fractional string is working correctly.
    Norbert_B
    i dont need to convert it back to string that i am doing only to check why it is showing zero value on converting number to string , because when i was writing to spreadsheet file it was always writing zero value, so when checked that code it was observed that while converting from array of numbers to spreadsheet string it is always writing zero value that's why i am doing it here just to check why it is not converting to string, but your suggestion helped in focussing the string that i am gettong from VISA read, i can use that  in last case if i wont be able to find the solution but main problem is that why i am not able to convert it back to string, why it is showing zero because it is because of this only write to spreadsheet is always showing zero value .

  • [svn:fx-trunk] 11642: Simple fix for non-integer translations by first multiplying to twips before casting to int .

    Revision: 11642
    Author:   [email protected]
    Date:     2009-11-10 19:40:31 -0800 (Tue, 10 Nov 2009)
    Log Message:
    Simple fix for non-integer translations by first multiplying to twips before casting to int.
    QE notes: Please test non-integer translations in FXG.
    Doc notes: N/A
    Bugs:
    SDK-24110 - Drawing of compiled FXG items is off by half a pixel
    Reviewer: Peter DeHaan
    Tests run: Checkintests, Bug test case
    Is noteworthy for integration: No
    Ticket Links:
        http://bugs.adobe.com/jira/browse/SDK-24110
    Modified Paths:
        flex/sdk/trunk/modules/fxgutils/src/java/com/adobe/internal/fxg/types/FXGMatrix.java

    i dont know why you are so mean i just asked which
    wrongs i had wrong
    anyway im done with thoseSo, the questions have 4 alternative answers. We tell you which questions you got wrong. This eleminates one of
    the alternatives, leaving 3 choices. Usually in most mutlichoice questions 1 choice is obviously incorrect,
    leaving 2 choices. One of those you have probably already discarded when you initially made your incorrect chocie
    thus leaving 1 alternative. Congratulations you just 100% on your test. I don't think so. Go read your book or notes or
    whatever you have because all the answers will be in there. The teacher will not ask you a question on a subject
    you have no prior knowledge of.

  • How to convert an int array to string array?

    Hi,
    Can anyone please help me with this question?
    int number={4,6,45,3,2,77};
    I like to convert this into a list and sort
    List mylist = Arrays.asList(number);
    Collections.sort(mylist);
    But first I need to convert the int array to String array.
    Please advise. Thanks.

    If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
    However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
    Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
    /Stephane

  • Relating int's to string

    how would i relate an int to a string. so for example, if i were playing a game where i have a row of 6 stars
    then i prompt the user for a number to remove stars(i can do this)
    say he says 4
    how would i make it so then i could change that
    to
    **

    It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.
    Java� API Specifications
    Java� 1.5 JDK Javadocs

  • How to get String out of  void method

    I have a problem to get String out of void method. I override method actionPerformed(), and I need to use String from this method in other methods. I get nothing in smth. Can anyone help me with this.
    class
    StringBuffer smth = new StringBuffer();
    StringBuffer s;
          public void actionPerformed(ActionEvent event){
              String vnos;
              if(event.getSource() == btn_vnos){
                   input = field.getText();
                   s = new StringBuffer(input);
                    smth = s;
                }             Thanks

    You have needs to use a static variable or somehow pass an instance reference, through a list or array or some such thing, perhaps through the constructor. Or think up some other jazz, like adding a method "public String getMyStinkin'String" ;~)
    ~Cheers

  • What is the use of passing String[] args in main() method?

    what is the use of passing String[] args in main() method?
    Is there any specific use for this ?

    actually my sir asked me the same question & I gave
    the same reply as given by you........but he further
    asked what is the use of this also??
    ie accepting cmd line args at runtime??is there any
    specific purpose ??Apart from the one you just mentioned? No

  • [svn:bz-trunk] 22381: Add some unit tests for the flex.messaging.client. FlexClientManager, including one for the new getFlexClient(String id, boolean createNewIfNotExist) method signature.

    Revision: 22381
    Revision: 22381
    Author:   [email protected]
    Date:     2011-09-02 05:10:41 -0700 (Fri, 02 Sep 2011)
    Log Message:
    Add some unit tests for the flex.messaging.client.FlexClientManager, including one for the new getFlexClient(String id, boolean createNewIfNotExist) method signature.
    Added Paths:
        blazeds/trunk/modules/core/test/src/flex/messaging/client/
        blazeds/trunk/modules/core/test/src/flex/messaging/client/FlexClientManagerTest.java

  • How to parse if input by user is a int or a string.

    EDIT: First issue has been fixed, now i'm trying to figure out how to make the program look at the user input. There where the user should input a number (E.G. What is you're first guess line, etc). I want the program to reconize when the input is NOT a number and to tell the user that the input is wrong, to enter a number between 1 and a 100. I don't mean to write an if statement that will tell the program number > or < than this number. I want it just to give a message out ONLY when the input is NOT a number but words symbols etc, whenever they are NOT integers. I'm smashing my head trying to figure out this one ;_; please help. Here is the updated code so far.
    import java.util.Scanner;
    import java.util.Random;
    public class Assignment3b
        public static void main(String[]arg)
            // Defining a new Scanner object and a new Random object
            Scanner kb = new Scanner(System.in);
            Random random = new Random();
            // Defining replay variables
            char replay = 0;
            String input;
            // Main game loop
            do
                // Generate random number
                int randNumber = random.nextInt();
                int maxvalue = 100;
                randNumber = random.nextInt(maxvalue) + 1;
                // Introduce the user to the game and ask for an initial guess
                System.out.println();
                System.out.println("Let's play a game.");
                System.out.println("I'll pick a number between 1 and 100, and you try to guess it.");
                System.out.print("\nWhat is you're first guess? ");
                int number = kb.nextInt();
                // Begin the "For Loop" which will ask the user to try again 9 additional times for a guess
                // depending on the user's input.
                for(int tries = 1;tries <= 10; ++tries)
                    if (tries == 10)
                        System.out.println();
                        System.out.println("Sorry, you didn't guess the number in 10 guesses.");
                        System.out.println("You lose, my number was " + randNumber);
                        System.out.println();
                        // Consume the remaining newline
                        kb.nextLine();
                        // Ask user to play again
                        System.out.print("Would you like to play again (y/n)? ");
                        input = kb.nextLine();
                        replay = input.charAt(0);
                        // Condition if user chooses to end the game at this point
                        if (replay == 'N' || replay == 'n')
                            System.out.println("\nThank you for using my guessing game program.");
                            System.out.println("Have a nice day.");
                            System.out.println();
                            System.exit(0);
                    // If the user chose a number too low to the one generated randomly, display this.
                    else if (number < randNumber)
                        System.out.print("That's too low, guess again: ");
                        number = kb.nextInt();
                    // If the user chose a number too high to the one generated randomly, display this instead.
                    else if (number > randNumber)
                        System.out.print("That's too high, guess again: ");
                        number = kb.nextInt();
                    // If the user chose a number that matches the one generated randomly, the program will take the following actions
                    else if (number == randNumber)
                        // Consume remaining newline
                        kb.nextLine();
                        // If user has guessed the number, display this and ask to play again.
                        System.out.println("\nYou got it in " + tries + " guesses! My number was " + randNumber);
                        System.out.print("Would you like to play again (y/n)? ");
                        input = kb.nextLine();
                        replay = input.charAt(0);
                        // Condition if the user chooses to end the program here
                        if (replay == 'N' || replay == 'n')
                            System.out.println("\nThank you for using my guessing game program.");
                            System.out.println("Have a nice day.");
                            System.out.println();
                            System.exit(0);
                        break;
                  // Condition that will cause the game to restart indefinitly until the user chooses to end the game.
                } while (replay == 'Y' || replay == 'y');
    Thanks a lot in advance :)
    Edited by: 805148 on Oct 25, 2010 6:49 PM

    Thanks i'll be looking into this. By the way, I do seem to get the grasp around the programming ambiance that it is favored to hint and help as to make people think for themselves furthermore. I am certainly not reluctant to think, but I have been spending many hours on all these small details, there's only so much I can do when I have been in computer science for 7 weeks learning basic java programming. I can check my book check the net, it's not about being lazy I can think all I want, but if i don't know certain codes or methods, simply because I didn't learn them and am unable to find them via my book or the net because I can vaguely type righteously what exactly I am searching for, I really can't do much more but ask for someones assistance which in return I do expect to be more concise rather than a vague hint to help me push the wheel further. For some I may understand, but I ask questions really only when I have juiced my brain out. I like figuring things out on my own but you can't figure everything on you're own.
    Regardless thanks for the suggestion i'll be looking into it now that I have something a bit more concise to work with.

Maybe you are looking for

  • Error while opening database studio in maxdb

    Hi Experts, I am having maxdb 7.8 and database studio AP MaxDB Database Studio,Version: 7.8.02.21,Build: 021-121-242-175 but when i am tying to open database studio it through error message " problems opening perspective 'com.sap.sdb.ui.perspectives.

  • Friend bars in Apple Stores UK ?

    I read about the new Friend bars in Apple stores in the U.S and I think it is a great idea. Is there any information about applying the same concept in the UK?

  • How to determine the Class of a static methods class?

    hi, is there a way to get the code below to output foo() called on One.class foo() called on Two.classthanks, asjf public class Two extends One {    public static void main(String [] arg) {       One.foo(); // should say "foo() called on One.class"  

  • Problem seeing a jframe components

    Hello, My problem is this. I have a main jframe. Now on my main jframe i have to do a time expensive procedure called refresh(); While this procedure is running i want to display a message in a JWindow that says "Please wait". Problem is that no matt

  • Microsoft Bluetooth Notebook Mouse 5000 continually drops and wont reconnec

    I have a Microsoft Bluetooth Notebook Mouse 5000 thats a few years old, but with my new OSX 10.6 MacBook Air, it keeps dropping the connection to the mouse. I can't tell if its when i close the lid all the time, sometimes it wont pick it up even when