Results 1 to 4 of 4
Thread: What am I doing wrong?
- 04-11-2011, 05:39 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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);
}
}
- 04-11-2011, 05:47 AM #2
- 04-11-2011, 06:34 AM #3
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.
- 04-11-2011, 05:35 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.Java Code:for(int i = 0; i < tokens.length; i = i + 4) { codeArray[i] = tokens[i].charAt(0); code.append(Character.toUpperCase(codeArray[i])); }
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
Please help, what am I doing wrong?
By mmac1218 in forum New To JavaReplies: 2Last Post: 09-01-2009, 11:21 PM -
what is wrong?
By pinguxxx in forum Advanced JavaReplies: 3Last Post: 07-15-2009, 12:33 PM -
what wrong
By pro85 in forum New To JavaReplies: 3Last Post: 02-09-2009, 01:07 PM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 08:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks