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 8 of 8

Thread: Input file to Array

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input file to Array

    Hi everyone

    I'm currently working on a program that will read in a file, break it up into sentences, check for duplicates (and print them to an outfile) and pull out specific sentences. I was trying to have the sentences saved as a String and then inputting each String into an Array (using ".", "!", etc. as cut off points so the program knows when the sentence is finished). For the duplicates, I was planning on counting the frequency of each sentence and print out the ones that occur more thna once. I was doing it this way as I also have to count the frequency of each word and pring the 100 words that occur more often, so I want to reuse as much code as possible.

    This is what I have so far
     
    	import java.io.*;
    	import java.util.*;
     
    	public class four {
    	public static String[] Array = new String[5000];
    	public static int[] Frequency = new int[500000];
     
    	public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new FileReader("X:\\Desktop\\book.txt"));
    	StringBuilder sb = new StringBuilder();
     
    	BufferedWriter bw = new BufferedWriter(new FileWriter("X:\\Desktop\\new.txt"));
     
        String line = br.readLine();
        while (line != null) {
            sb.append(line + "\n");
            line = br.readLine();
        }
        br.close();
        String text = sb.toString();
        getSentences(text);
    	int nbWords = 0;
    	int Sentences = 0;
    	}
     
    		public static int getSentences(String word) {
    		int i = 0;
    		String abc = "";
    		int result = 0;
    		char[] chars = word.toCharArray();
     
    		for(Character c : chars)
    		{
    			abc += c;
    			if(c == '.' || c == '!' || c == '?' || c == ']' /*|| c == 'k'*/)
    			{
    				Array[i] = abc;
    				abc="";
    			}
    			i++;
     
    		if (Array[i].isFull()==true)
    		{
    			writeline(Array[i]);
    		}
    	for (int n=0; n<Array.length; n++)
    	{
    		for (int j=0; j<Array.length; j++)
    		{
    			if (Array[n]==Array[j])
    			{
    				Frequency[n]++;
    			}
    		}
    	}
    	for (int p=0; p<Array.length; p++)
    	{
    		if (Frequency [p] > 1)
    			System.out.println(Array[p] = "=" + Frequency[p]);
    	}
     
    		System.out.println(Array[23] + Array [189] + Array[590] + Array[690] + Array [847]);
    		return result;
    	}
    }
    }

    Any input anyone could give would be greatly appreciated! I usually use Perl so I'm out of my league with all this Java.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input file to Array

    I am really not sure what your question is. Does that code compile? Your indentation and lack of standard naming conventions (methods and variables start with a lower case letter, classes with upper case) makes it pretty hard to read.
    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
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file to Array

    It compiles sometimes but doesn't print anything to screen, currently it's reporting problems with "writeline" and with the Reader and Writer, specifically declaring new FileReader and new FileWriter.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input file to Array

    What do you mean that it compiles sometimes? And what are the problems it is reporting? Copy and paste any compiler errors or stack traces you get, including the lines referenced.

    I really suggest you reformat your code, as that weird indentation cannot be helping you out any.
    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!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file to Array

    Okay well I altered the code slightly
     
    	import java.io.*;
    	import java.util.*;
     
    	public class four {
    	public static String[] Array = new String[5000];
    	public static int[] Frequency = new int[500000];
     
    	public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new FileReader("X:\\Desktop\\book.txt"));
    	StringBuilder sb = new StringBuilder();
    	FileWriter fstream = new FileWriter("new.txt");
    	BufferedWriter bw = new BufferedWriter(fstream);
    	//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("X:\\Desktop\\new.txt")));
     
        String line = br.readLine();
        while (line != null) {
            sb.append(line + "\n");
            line = br.readLine();
        }
        br.close();
        String text = sb.toString();
        getSentences(text);
    	int nbWords = 0;
    	int Sentences = 0;
    	}
     
    		public static int getSentences(String word) {
    		int i = 0;
    		String abc = "";
    		int result = 0;
    		char[] chars = word.toCharArray();
     
    		for(Character c : chars)
    		{
    			abc += c;
    			if(c == '.' || c == '!' || c == '?' || c == ']' /*|| c == '".'*/)
    			{
    				Array[i] = abc;
    				abc="";
    			}
    			i++;
     
    		if (Array[i]!=null)
    		{
    			out.Write(Array[i]);
    		}
    	for (int n=0; n<Array.length; n++)
    	{
    		for (int j=0; j<Array.length; j++)
    		{
    			if (Array[n]==Array[j])
    			{
    				Frequency[n]++;
    			}
    		}
    	}
    	for (int p=0; p<Array.length; p++)
    	{
    		if (Frequency [p] > 1)
    			System.out.println(Array[p] = "=" + Frequency[p]);
    	}
     
    		System.out.println(Array[23] + Array [189] + Array[590] + Array[690] + Array [847]);
    		return result;
    	}
    }
    }

    I get this problem under "Build Output" -
    --------------------Configuration: <Default>--------------------
    C:\Users\yr411018\Desktop\four.java:46: cannot find symbol
    symbol : variable out
    location: class four
    out.Write(Array[i]);
    ^
    1 error

    Process completed.

    And under "General Output" I get -
    --------------------Configuration: <Default>--------------------
    java.lang.NoClassDefFoundError: four
    Caused by: java.lang.ClassNotFoundException: four
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: four. Program will exit.
    Exception in thread "main"
    Process completed.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Input file to Array

    And when do you declare and initialize out?
    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!

  7. #7
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Input file to Array

    import java.io.*;
     
    import java.lang.*;
     
     
     
    public class latarray{
     
           public static void main(String [] args)throws IOException
     
                  {                         
     
                  BufferedReader baca=new BufferedReader(new InputStreamReader(System.in));
     
                         int i;
     
                         int[] no=new int[5];
     
                         System.out.println("Input Data");
     
                         for (i=0; i<5; i++)
     
                               {
     
                               //System.out.println("Input Data");                   
     
                               no[i]=Integer.parseInt(baca.readLine());
     
                               }
     
                         for (i=0; i<5; i++)
     
                               {
     
                               System.out.println(no[i]);
     
                               }
     
                  }
     
    }

  8. #8
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Input file to Array

    Quote Originally Posted by erdy_rezki View Post
    import java.io.*;
     
    import java.lang.*;
     
     
     
    public class latarray{
     
           public static void main(String [] args)throws IOException
     
                  {                         
     
                  BufferedReader baca=new BufferedReader(new InputStreamReader(System.in));
     
                         int i;
     
                         int[] no=new int[5];
     
                         System.out.println("Input Data");
     
                         for (i=0; i<5; i++)
     
                               {
     
                               //System.out.println("Input Data");                   
     
                               no[i]=Integer.parseInt(baca.readLine());
     
                               }
     
                         for (i=0; i<5; i++)
     
                               {
     
                               System.out.println(no[i]);
     
                               }
     
                  }
     
    }
    what the function of import java.lang.*; ?

Similar Threads

  1. Input into array.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 22
    Last Post: May 18th, 2012, 09:12 AM
  2. Input in a two Dimensional Array.
    By Mr.777 in forum File I/O & Other I/O Streams
    Replies: 29
    Last Post: December 14th, 2011, 11:40 PM
  3. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  4. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 01:05 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM