Results 1 to 4 of 4
  1. #1
    javajen is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default What am I doing wrong?

    What am I doing wrong? the txt file is attached........

    Write a main method that will read the file secret.txt, separate it into word
    tokens. You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to a StringBuilder object to form a word which will be printed to the console to display the secret message.



    import java.util.StringTokenizer; //to use the string tokenizer class
    import java.io.*;
    import java.util.Scanner;

    public class SecretJava {

    public static void main (String[] args) throws IOException
    {
    File file = new File("C:\\java\\bin\\secret.txt");
    Scanner secretFile = new Scanner(file);

    String line;

    line = secretFile.nextLine();

    StringBuilder code = new StringBuilder();

    String[] tokens = line.split(" ");

    char[] codeArray = new char[tokens.length];

    for(int i = 0; i < tokens.length; i = i + 4)
    {
    codeArray[i] = tokens[i].charAt(0);
    code.append(Character.toUpperCase(codeArray[i]));
    }

    System.out.println(code);
    }

    }
    Attached Files Attached Files

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Quote Originally Posted by javajen View Post
    What am I doing wrong?
    since we don't read minds you need to tell us. Do you get compiler errors? Incorrect output/behaviour. Provide details.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    The more details you provide about whats going wrong with your code the more help we can give. If you get an error post it here, if it compiles and run then show us what you get and what you're supposed to get. That way we can analyze what might be going wrong.

    The more information we have the easier it is to narrow down the snippet of code thats causing a problem.

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    for(int i = 0; i < tokens.length; i = i + 4)
    {
    codeArray[i] = tokens[i].charAt(0);
    code.append(Character.toUpperCase(codeArray[i]));
    }
    This will look at the elements of codeArray in the order 0, 4, 8, 12 [...]. You want to be looking at 0, 5, 10, 15 [...]. I can't guarantee that that's the only thing wrong, but it's the only thing I noticed straight away - and, as the others are saying, it helps if you're more specific with your problems.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Please help, what am I doing wrong?
    By mmac1218 in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 11:21 PM
  3. what is wrong?
    By pinguxxx in forum Advanced Java
    Replies: 3
    Last Post: 07-15-2009, 12:33 PM
  4. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 01:07 PM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 08:35 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
  •