Results 1 to 8 of 8
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default problem with for loop

    I am writing a program in Java that reads a text file, and outputs its contents to the screen, one word at a time.

    So I have this code, which works:

    Java Code:
    import java.io.FileReader;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    class cmprsr{
    	private static Scanner scan;
    	private static StringTokenizer st;
    	private static String[] wordList;
    	private static String[] temp;
    	private static int[] intArr;
    
    	public static void main(String [] args){
    		try{
    			String stringCount = null;			
    			String s = null;
    			int wordCount = 0;			
    			scan = new Scanner(new FileReader("story.txt"));
    			while(scan.hasNext("\\S+")) {
    				stringCount = scan.next("\\S+");
    				wordCount++;
    			}
    			wordList = new String[wordCount];
    			temp = new String[wordCount];
    			intArr = new int[wordCount];
    			scan = new Scanner(new FileReader("story.txt"));
    			while(scan.hasNextLine()){
    				st = new StringTokenizer(scan.nextLine());
    					while(st.hasMoreTokens()){
    						s = st.nextToken();
    						//insert code here (see below)
    						System.out.println(" " + s);
    					}
    			}
    		}catch(Exception e){}
    	}
    }
    The above code works perfectly; it prints the contents of the text file to the screen, one word at a time. And now I want to check whether the string contained in s is already in my wordList array..

    But when I try to add the following code at the point I've labelled "//insert code here", the program no longer outputs anything to the screen.

    Java Code:
    for(int i = 0; i < wordList.length; i++){
    	if(wordList[i].compareTo(s) == 0){
    		int a = 0;
    	}
    	else{
    		int a = 1;
    	}
    }
    I have also tried this:

    Java Code:
    for(int i = 0; i < wordList.length; i++){
    	if(wordList[i].compareTo(s) == 0){
    		int a = 0;
    		break;
    	}
    	else{
    		int a = 1;
    	}
    }
    The program still compiles just fine with the 'javac' command, and I recieve no errors while running the program in the terminal/command prompt. But I want it to still output the text file's contents to the screen, which it does not.

    Any useful advice is much appreciated. And thank you in advance for taking the time to at least look through my code and try to help.
    Last edited by cherrychives; 04-23-2012 at 12:40 PM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: problem with for loop

    Please use the code tags when posting code on the forum - that makes it readable for us... further I do not see the label "Insert code here" you mentioned anywhere

  3. #3
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: problem with for loop

    Sorry. Can't believe I forgot to put that in.

    Sorry about the code tags; as you can see, this is my first post, so I didn't know. I think I fixed that now..
    Last edited by cherrychives; 04-23-2012 at 12:26 PM.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: problem with for loop

    No problem - you have forgotten to initialize the content of your "wordList" ... ;)
    You get probably a NullPointerException...

  5. #5
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: problem with for loop

    I see.. I didn't know you couldn't do that.. I thought it would just compare s with the null variables anyway. Guess not.

    Thanks for the answer. Really helped a lot.

  6. #6
    Govind is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Re: problem with for loop

    Hi every , I need some fromat of java code i want use in billing application
    My program output is
    12.90
    1234.90
    but i am execepting output
    12.90
    1234.90

    please tell me the code as early as possible

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: problem with for loop

    @Govind: Make an own thread - do not post into other people's threads, this one is already solved!
    Second: No one will post you code, you should be able to develop this on your own. We help if you have a problem with your implementation. Right now I guess it is fine as it does what it is expected to do. :)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: problem with for loop

    Quote Originally Posted by cherrychives View Post
    Java Code:
    		}catch(Exception e){}
    The program still compiles just fine with the 'javac' command, and I recieve no errors while running the program in the terminal/command prompt.
    No kidding.
    You catch all exceptions in that catch block and do nothing with them, and you;re surprised you get no errors when you run the code?
    You are doing what's called "eating exceptions".
    AT the least you should be doing 'e.printStackTrace()' in there so you will know when, what and exactly where an exception happens.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Problem in while loop
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 7
    Last Post: 04-21-2011, 07:19 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 03:10 AM
  5. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM

Tags for this Thread

Posting Permissions

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