Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Arranging words in a sentence in alphabatical order.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arranging words in a sentence in alphabatical order.

    Each sentence begins with a space. Gettin a null pointer exception at line 55. Plez help.


    import java.io.*;
     
    public class sent5 {
    	String temp = "";
    	String x;
    	String x1; // to store each word of sentence
    	String c;
    	int l; // length of string
    	int i;
    	int l1; // length of sentence
    	int c1;
    	int k = 0; // array variable
     
    	public void test() throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("ENTER SENTENCE");
    		x = br.readLine();
    		x += " ";
    		l = x.length();
    		for (i = 0; i < l; i++) {
    			if ((x.charAt(i) != '.') && (x.charAt(i + 1) != ' '))
    				c += x.charAt(i);
    			else
    				sent(c);
    		}
    	}
     
    	public void sent(String c) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		l1 = c.length();
    		for (i = 0; i < l1; i++) {
    			if (x.charAt(i) == ' ')
    				c1++;
    		}
    		String v[] = new String[c1];
    		for (i = 0; i < l1; i++) {
    			if (c.charAt(i) != ' ')
    				x1 += c.charAt(i);
    			else {
    				v[k] = x1;
    				k++;
    				x1 = null;
    			}
    		}
    		for (int i = 0; i < c1 - 1; i++) {
    			for (int j = 0; j < c1 - i - 1; j++) {
    				if (v[j].compareToIgnoreCase(v[j + 1]) > 0) {
    					temp = v[j];
    					v[j] = v[j + 1];
    					v[j + 1] = temp;
    				}
    			}
    		}
     
    		// print
    	}
     
    	public static void main(String args[]) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		sent5 o1 = new sent5();
    		o1.test();
    	}
    }
    Last edited by KevinWorkman; February 24th, 2011 at 07:44 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,459
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: Arranging words in a sentence in alphabatical order.

    Which line is line 55? Where is your stack trace?

    When posting code, please use the code or highlight tags. I did it for you this time, but please be more mindful in the future. You should probably edit your post and fix the indentation.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Arranging words in a sentence in alphabatical order.

    I've fixed the indentations as I compiled the code. As Kevin says, please do this yourself in future.

    You have several issues but the first one starts here:

    if (v[j].compareToIgnoreCase(v[j + 1]) > 0) {

    Read up on the NullPointerException - http://download.oracle.com/javase/6/...Exception.html

    If I were you I would remove everything else and concentrate on getting this part of the code to work before you move on.
    Another tip would be to give your variables a more descriptive name. l, i, k, c1 etc makes things more complicated IMO
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Sort in Cyrilic order
    By cselic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2010, 03:08 PM
  2. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 01:46 PM
  3. making a sentence proper
    By dvsumosize in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 3rd, 2010, 10:01 AM
  4. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM
  5. reading a sentence
    By lotus in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 20th, 2009, 08:29 AM