Results 1 to 6 of 6
  1. #1
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default Scanner Class...

    Hey,

    I am making a program which asks the user to input a word and then a sentence, the program then takes the word and inputs in at a certain location in the sentence. Example:

    String word = “example”;
    String sentence = “An example sentence”;

    String one = word(sentence,word,0);
    “example An example sentence”

    String one = word(sentence,word,3);
    “An example sentence example”

    I am stuck on how to get the program to do this any ideas?

    Thanks.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you could create an array of Strings and use Delimeter attached to your in.next() that will take each word and put it into the array. After that it's as easy as using Array.set(position, word) (I think thats the method you will need to look it up).

    Pretty much you just tell it the word and position in the array to set the word and it will automatically re-organize everything

  3. #3
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    I would use an array and a few methods of the ArrayList class. you can create an object of that class and then simply add strings to an array. you can also place strings into a specific element of an already created and initialized array.

  4. #4
    TheRocket is offline Member
    Join Date
    Nov 2008
    Posts
    20
    Rep Power
    0

    Default

    Hmm, arrays do sound easy any idea how I would go about doing this. If you could create a basic example that would be great.

    Thanks

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Array[] someArray = new Array[10];

    that would create an array with 10 spots[0-9] allocated in memory for use. Read the java docs on delimiter and the String api to get some really useful methods.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Java 2 Platform SE 5.0

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation [] not Array

    Quote Originally Posted by xcallmejudasx View Post
    Java Code:
    Array[] someArray = new Array[10];
    Java Code:
    C:\src_jdk1.5.0_12\java\lang\AbstractMethodError.java
    C:\src_jdk1.5.0_12\java\lang\AbstractStringBuilder.java
    C:\src_jdk1.5.0_12\java\lang\Appendable.java
    C:\src_jdk1.5.0_12\java\lang\ArithmeticException.java
    C:\src_jdk1.5.0_12\java\lang\ArrayIndexOutOfBoundsException.java
    C:\src_jdk1.5.0_12\java\lang\ArrayStoreException.java
    C:\src_jdk1.5.0_12\java\lang\AssertionError.java
    C:\src_jdk1.5.0_12\java\lang\AssertionStatusDirectives.java
    C:\src_jdk1.5.0_12\java\util\Arrays.java
    C:\src_jdk1.5.0_12\java\util\ArrayList.java
    Where's
    Java Code:
    class Array{}
    ?

    nope, it's [] pretty much anything can have [] which makes an array of whatever it is close to.

    I prefer the [] to be appended to the type as the varable is an array of the type declared to be arrayed.

    Note that this declaration does not put anything in the array, to do that we either use a loop or in some fashion put Objects or primitive in each unit of the array.

    In general, do if((array = null)&&(array.length > 0)&&(array[0] > minValue)&&(array[0] < maxValue)){......}else{error} then wrap the whole code block in a try / catch .....

    To do anything cheaper than that will keep you buying books at the mall and placing them to impress people, rather than getting shippable code.

    Remember, it always fails on rollout to your boss's most important customer: Even the Great Gates had a bluescreen of death on a network feed.

    Code accordingly or keep your resume for flippin burgers current.
    Last edited by Nicholas Jordan; 12-05-2008 at 09:49 AM. Reason: Array / Arrays
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Scanner Class Performance?
    By jazz2k8 in forum New To Java
    Replies: 1
    Last Post: 05-14-2008, 02:42 AM
  2. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 12:41 AM
  3. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •