Results 1 to 6 of 6
  1. #1
    Tussmann is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Space for charAt()?

    greetings, im new to Java, and is programming in Dr.Java.
    Im making a program that reads a string, and has a line shift everything the character "|" appears in the string. But I can't seem to get past the basic problem, og a simple space..
    For eksample, if i type "A new day|its nice!", the program should print it as:
    "A new day
    its nice!"

    instead, my program prints : "A", and thats it.. it stops when a space occurs..

    The program is:

    import java.util.Scanner;
    class oppgave13 {
    public static void main (String [] args) {
    Scanner tastatur = new Scanner(System.in);

    char tegn;
    String setning = tastatur.next();
    for (int i = 0; i < setning.length(); i++) {
    tegn = setning.charAt(i);
    if (tegn == '|') {
    System.out.println(); }
    if (tegn == ' ') {
    System.out.print(" "); }
    else {
    System.out.print(tegn); }
    }
    }
    }


    Im pretty sure that this is the wrong one, but I can't seem to find a way to get the program to ignore the space and just keep on going with the other characters.. Any tips, oh great java gods?

    Mr.T.

  2. #2
    Tussmann is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    And by this, I mean this:
    if (tegn == ' ') {
    System.out.print(" "); }

    ;)

    Mr. T

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    why do you bother checking if its a space?
    Why not just
    Java Code:
    if (tegn == '|') {
        System.out.println(); 
    } else { 
        System.out.print(tegn);
    }
    regardless, please surround your blocks with [ code] tags
    it makes it easier to read :)

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    In answer to your actual question, you need to look at the docs for the Scanner class.
    Scanner (Java 2 Platform SE 5.0)

    If you look at the method next(), it returns the next token. By default, tokens are separated by white space, which will include spaces.
    You should probably use the nextline() method

  5. #5
    Tussmann is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Ah, sorry about that, new to the place ;) Will remember to do that next time ;)

    As for the nextLine(), that did the trick. Thanks man ;) Read through a couple of the scanner class methods, and I see where I went wrong ;)
    Again, thanks.

    Mr. T

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

Similar Threads

  1. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 05:27 PM
  2. using String.charAt() for semicolons
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 01-11-2010, 01:40 PM
  3. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  4. Palindrom - method charAt()
    By user in forum New To Java
    Replies: 10
    Last Post: 11-16-2008, 04:37 AM
  5. Help With Input.charAt(LastIndex);
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:22 AM

Posting Permissions

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