Question on Strings

I get stuck in an infinate loop here, and I'm not sure why. When user input is the word "done", it's not registering. Any ideas would be appreciated.
while ( stName != "done" )
stName = JOptionPane.showInputDialog(" Enter Student name: ");
if (stName != "done")
courseInfo.regName( stName );
-Thank you

==
but as it is in a if statment, you can do
if( ! string.equals( "Something" ) ) {
}You might also like the equalsIgnoreCase( )
Reources for Beginners
http://java.sun.com/docs/books/tutorial/
http://java.sun.com/learning/new2java/index.html
http://javaalmanac.com
http://www.jguru.com
http://www.javaranch.com
Bruce Eckel's Thinking in Java
Joshua Bloch's Effective Java
Bert Bates and Kathy Sierra's Head First Java

Similar Messages

  • Question about string manipulation

    Hello, I am practicing with Java and trying to learn it, and have a quick question. I am trying to get the count on a string of numbers for after a decimal point. the numbers are generated as the result of a math, so I can't use index, since the location of the decimal changes based on the number imputed. I want to figure this out on my own, but have hit a wall here, i've spent the past few hours trying every string command I can think of, and haven't figured it out yet, anyone mind pointing me in the right direction for which string to use? Thanks in advance for any help you can provide!

    Is this what you want?
    public class Fallen{
      public static void main(String[] args){
        String number = "123.45678";
        String frac = number.substring(number.indexOf('.') + 1);
        System.out.println(frac + " length=" + frac.length());
        frac = number.replaceFirst("^.+\\.", "");
        System.out.println(frac + " length=" + frac.length());
    }

  • Question abotu String.format() functionality

    Hello all,
    I am curious as to whether this functionality exists in the String.format method, or if there is any other class in the current version of Java that would allow for this.
    I want to construct a format string, where if one of the args contains a newline character in it, the same format for that arg will be used on the newline.
    Example:
    String s = String.format("%20s%20s","this line contains\na newline character","so does\nthis one");
    System.out.println(s);
    Actual Output:
    this line contains
    a newline character    so does
    this one
    Wanted Output:
    this line contains         so does
    a newline character      this oneHopefully there is something that exisits in the language that would allow me to do this. If no one can think of any easy way to do this? I have a few ideas, but it would require a whole lot of string manip. than I want to put into it =(
    Anyways, I look forward to your reply. Thanks!

    You want the method to treat the strings as multi-line blocks of text instead of as strings? And put the blocks next to each other instead of the strings? Maybe padding the blocks with spaces? Forget it, that's way too complicated for a quick and dirty formatting method.
    And yes, a whole lot of string manipulation would be going into that.
    Assuming that I understood your requirements correctly. I just went by the picture, I couldn't make any sense out of the words you used to describe it, so I may well be totally wrong.

  • Question about String constant pool

    Is there is any function available to find how many Sring literal in String Constant pool

    Well, there's stuff like BCEL.
    But it may not do what you want.
    Why do you want to find out what's in the constant pool?

  • Quick Question on String Searches

    I'm about to write a small program to find a target string in any file format, then display the target string and the first 200 characters before and 200 characters after the target string to the console. I want to use OOP, using main() for testing purposes only, but I'm not sure of the best way to approach this. Should I use the store() and load() methods from Properties or just use indexOf()? Any thoughts, folks? Thanks.

    If fyou are going to be searching in any file format (including binary files such as Word docs) then you can't really use the indexOf or the Properties class (I am not sure what your intentions were with the Properties class, but using it requires a particular file format).
    Just off the top of my head, I would suggest using a RandomAccessFile and seeking around, reading data into buffers when you find what you want. To me, this isn't exactly a job for OOP, at least at its guts. You can use OOP to come up with a design - perhaps using specific "Searchers" tuned to particular file formats, and then have a driver class that determines, at runtime, which Searcher class to use for a given file. But again, that's just off the top of my head.
    Good Luck
    Lee

  • Easy question about String

    Hi!
    I have a string in a variable. how can I check if the String is composed just by numbers?
    thank you very much!

    Use the following method from Integer API
    parseInt
    public static int parseInt(String s)
                        throws NumberFormatExceptionParses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.If it throws NumberFormatException its alphanumeric else its numeric. You would however have to remove any spaces or commas etc. since they are not mumeric either

  • A question regarding String

    One String problem confused me.
    String s = "aacc";
    Now, i want to insert "bb" between "aa" and "cc", The result would be that s equals "aabbcc". I had thought to implement it by using insert method. but I was surprised that Class String do not support this method. Could anyone give me a simple way that can do this. thanks a lot.

    Use the StringBuffer class instead. The String class is imutable. It will create a new string when modified.
    StringBuffer b = new StringBuffer("aacc");
    b.insert(2, "bb");

  • Noobish question on strings-plz help

    Whats wrong with this code?
    public String name(){
              String str="hi";
              if(str.equals("hi"))
                   return str;
    Why is it saying "This method should return a result of type string"?
    Regards.

    public String name(){
        String str="hi";
        if(str.equals("hi")) {
            return str;
        // if above if statement is false code gets to here
        // where is the return statement?
    }The compiler doesn't give a shit about evaluating the if statement to see that it can only be true. All the compiler looks at is that when the if statement is false there is no return statement.

  • A question about String.hashCode()

    Why the implementation of hashCode() for String class using 31 as a base?

    Why the implementation of hashCode() for String class
    using 31 as a base?I think it's a magic number. It has been found to produce a reasonably even distribution over the int range.

  • A question about string.trim()

    I got a fragment of code as follows:
    String s = "abcd ";
    boolean a = (s.trim()=="abcd");
    boolean b = (s =="abcd ");
    Why a is false, b is true?

    The reason why the below code has true assigned to be is quite easy to explain:
    String s = "abcd";
    String y = "abcd";
    boolean b = (s == y);
    ...The String class has a "Pool" of strings. When you use String literals; the compiler automatically checks if it is in the String pool already and if it exists; a reference to that String is returned; if not a new entry in the pool is added and then you get a reference to that. This is only true for Stirng literals and String created using the intern() method.
    If you compile this code you will see that the strings are not equal:
    public static void main(String args[])
      String s = "abcd";
      String y = args[0];
      boolean b = (s == y);
      System.out.println(b);
    }This is because the "abcd" is in the String pool when your program starts; but because the value "abcd" passed in (is not created in the pool automaticlaly). Therefor they are two different String objects.
    This code block shows that you can add Strings to the pool. The intern() method checks to see if the String is in the pool already and if it is it will return you a reference to it; if not it will create one and then return it.
    public static void main(String args[])
      String s = "abcd ";
      String y = args[0].intern();
      boolean b = (s == y);
      System.out.println(b);
    }- Chris

  • Question about string constant in Hex display format

    how to get a plus of 2 string constant which is display in Hex display like attahced, for example I have string "28" and "5D" in Labview which has been in Hex format, the plus of this 2 string should be "85" in Hex. pls help on this, thanks
    Attachments:
    1.jpg ‏21 KB

    This'll work, too.  I'm afraid of casting types...
    Jm
    Jim
    You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
    Attachments:
    hex.png ‏2 KB
    hex.vi ‏7 KB

  • A quick question of String

    how to check whether it starts with digitals or not? i remember there is a quick solution but i forget it.
    i checked the books but found no answer. could anybody tell me? Thanks!

    Maybe this will work:
    String s = "1abc";
    try{
    Integer.parseInt(s.substring(0,1));
    catch (NumberFormatException e){
    System.out.println("1st char is not a integer");

  • Question on String and '+'

    We all know String is not a primitive but actually a class in java, I guess. may be a wrapper class for char. array?
    anyway, what makes it possible for '+' sign to work on string concat.? Java have no operator overload, as I rememeber. so what actually is done when 2 'Class' are 'added' with a '+' sign? is there any hidden feature?
    Thanks for your time.
    cynip.

    We all know String is not a primitive but actually a
    class in java, I guess. may be a wrapper class for
    char. array?
    anyway, what makes it possible for '+' sign to work on
    string concat.? Java have no operator overload, as I
    rememeber. so what actually is done when 2 'Class' are
    'added' with a '+' sign? is there any hidden feature?
    Thanks for your time.
    cynip.The java compiler internally uses a StringBuffer to do string concatenation:
    http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html

  • Question on String constructors

    Hi.
    String is not a primitive type, so, like all not primitive types, new string objects must be instantiated calling a constructor, via the operator 'new0.
    However this "rule" is not valid with class String (and maybe with other classes), as it is possible to create new string objects simply using the form String myStr = "myStr";
    Well, I'd like to know how this is possible and which kind of statements I should use in my classes to support this kind of instantiation.
    Thanks!

    However this "rule" is not valid with class String
    (and maybe with other classes), as it is possible to
    create new string objects simply using the form
    String myStr = "myStr";You're not necessarily creating a new String object.
    String s1 = "hello";
    String s2 = "hello";
    System.out..println(s1 == s2); // test for objct identity will return truehttp://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5
    Well, I'd like to know how this is possible and which kind of
    statements I should use in my classes to support this kind of
    instantiation.If you mean you want that stuff for your own classes: you can't.

  • Question about String initialization~~~

    String a = "sss";
    String b = "sss";
    the result of a==b is true .
    String a = new String("sss");
    String b = new String("sss");
    however , the result of a==b is false .
    why? the operator "==" compares WHAT of two object ??
    thanks , ;[                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    == compare the reference of the object
    String s1 = new String("sss") creates a new String object at location xxxxxx, while
    String s2 = new String("sss") creates a new String object at location yyyyy.
    therefor xxxxxxx != yyyyyyyy
    by location..the memory address
    String s3 = "sss"; // java create a new String object "sss" at location zzzzzzzz and assign the reference to s3
    String s4 = "sss"; // java assign the reference (location zzzzzzzz) to s4
    so s3 == s4....
    noticed only one String object is created...s3 and s4 holds the reference to the 1st String created..though it is not clearly shown in th ecode...it actually look something like this
    String temp = new String("sss");
    String s3 = temp;
    String s4 = temp;

  • Simple question on Strings

    Is there a cleaner (and/or easier) way of adding a string value into another string? ie:
    String myString = "How You?";
    int i = myString.indexOf(" ");
    String a = myString.substring(0,i); //should equal "How"
    String b = myString.substring(i,myString.length()); //should equal " You?"
    String c = a + " Are" + c; //should equal "How Are You?"

    Sounds good to me.
    Although in this particular example you could have done either:String c = myString.replaceAll(" ", " Are "); // (1.4+)-or-String c = myString.replace(" ", " Are "); // (1.5+)Also there are the StringBuffer or StringBuilder (1.5+) classes which have an insert(int,String) method.
    Regards

Maybe you are looking for

  • Characters getting lost while calling a remote function

    Hi there, I've got a problem concerning a remote function call. Situation: System A has a function module ZFM_X with one importing parameter ("text" type string). System B calls this function module ZFM_X through a remote function call. It passes a s

  • Thumbnail of the pdf contains blank section where there were images. However, the text is displayed.

    Hi there, I was trying to get thumbnail images of the pdf pages using coldfusion 10 cfpdf. Thumbnails have been generated for most of the pdf files. However, for one particular pdf file, the text in the pdf file were displayed properly but the image

  • Abap interface

    hi:       I have a requirement to achieve SAP and external systems interfaces       However,  have large amount of data.       Which interface mode for high-volume data?(remote rfc  .webservice.......)       please give me suggestion.      thanks

  • Well, this is counter-intuitive!

    When I crop my pictures in Photoshop CS5.5 on Mac (10.7) the file size increases.  I was working in an appx. 500mb 16-bit PSD and after I cropped it (using the crop tool), the file size inflated to almost 1gb!  Why would that happen? thanks for you h

  • Replication Issue 'CON_NAME' unknown

    Hi, We have configured replication from SAP PO Java system as non-ABAP source, and the replication errors out with CON_NAME unknown error. Below is the application log: Z_BC_BPEM_ACL_025: Run 0000000001 aborted due to SYSTEM FAILURE at 20150427 09420