10 Replies Latest reply: Jan 28, 2008 8:22 PM by 807603 RSS

    How to retrieve a string?

    807603
      Basically im trying to create a database and i do not know how to retrieve a string from an input.
      An integer would be retrieved like
      number = scanner.nextInt();
      what i would like to know is, is there a process similar to this to retrieve a string from user input?

      Thanks
        • 1. Re: How to retrieve a string?
          807603
          There are similar methods that return a String, take a look through the API.

          http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
          • 2. Re: How to retrieve a string?
            807603
            I looked at it but it didn't tell me how i could save that as a variable

            It shows me how i can print it to screen but that means i can only use it once i cant save it to a variable called 'name' to use whenever i need it.

            any ideas?
            • 3. Re: How to retrieve a string?
              807603
              danieltnbaker wrote:
              I looked at it but it didn't tell me how i could save that as a variable

              It shows me how i can print it to screen but that means i can only use it once i cant save it to a variable called 'name' to use whenever i need it.

              any ideas?
              Huh? You would save it as a variable the same way as the code you originally posted.
              • 4. Re: How to retrieve a string?
                807603
                i tried that but the error message i said was

                incompatible types found: java.lang.string
                required char

                I dont understand the error my code looks like
                name = getname.next();
                can you see errors in my code?
                • 5. Re: How to retrieve a string?
                  807603
                  What did you declare name as? A String or a char?
                  • 6. Re: How to retrieve a string?
                    807603
                    my code is:-
                    public static void main(String[] args) {
                           
                            char name;
                            
                            System.out.println("\n---------------------------------");
                            System.out.println("\nWhat Is Your Name?");
                            System.out.println("\n---------------------------------");
                            
                            System.out.print("\n\nPlease Enter Your Name: ");
                            Scanner getname = new Scanner(System.in);
                            name = getname.next();
                            System.out.print("\n\nYou Said Your Name Was: " + name + "!");
                            System.out.print("\n\nHello " + name);
                        }
                    To store the variable as a string would i have to declare it as 'string name;'??
                    After using C++ i assumed like c a string is declared as a character.
                    • 7. Re: How to retrieve a string?
                      807603
                      There's a String class in java.

                      http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
                      • 8. Re: How to retrieve a string?
                        807603
                        Thankyou that page was a great help

                        never would of got that without your help!

                        many thanks
                        • 9. Re: How to retrieve a string?
                          807603
                          Spiced rum is always a good solution.

                          Especially eight of them.
                          • 10. Re: How to retrieve a string?
                            807603
                            I strongly recommend that you do a Java tutorial (the Sun Java tutorials are excellent) or go through a book in a stepwise fashion, otherwise you risk propagating bad assumptions and habits. Java isn't like C++, and you'll have to learn a new way of thinking.