Output Contents Of Input File In Alphabetical Order One Word Per Line

Jun 22, 2014

I am having a lot of trouble with this lab. basically I have to make a text processor to read in code put it into a file and output the contents of the input file in alphabetical order one word per line. I have to reference the string varaibles input_filename and output_filename.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
public class TextProcessor {

[Code] ....

these are the guidelines to said lab:

1. In the main method define two Strings called input_filename and output_filename. I will be setting these Strings to my own file names for testing your code, so make sure you get these variable names correct, and that you use them properly in the following steps.
2. Read in the text contents of the file referenced by input_filename.
3. Split the contents of the input text into separate tokens, using whitespace as a delimiter.
4. Lower case the tokens.
5. OPTIONAL: remove punctuation
6. Alphabetize the tokens.
7. Using output_filename, write the alphabetized tokens to an output file, one token per line.
8. Be sure to close file streams - no resource leaks!
9. Use methods to separate functionality in your program where possible

View Replies


ADVERTISEMENT

Display Contents Of File In Reverse Order - Output To Console

Jun 24, 2014

Create a class that allows the user to select a file and output the contents of the file to the console. Recommend using a text file to display, java files are text files. You may want to put this code in a method to allow you to complete the remainder of this assignment without overwriting your code.

- You should ask the user to enter the full path to your file to make certain you are processing the correct file.

- Modify your code/add a method to display the contents of the file in reverse order. You do not need to reverse the characters on each line but simply output the lines of the file in reverse order.

Hint: read the content of the file into an ArrayList and then traverse the ArrayList.

this is what i go so far.

package classActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Select
{
public static String enterFilePath()

[Code] ....

View Replies View Related

Sorting Array In Alphabetical Order

Feb 3, 2015

I am trying to sort an array that I have by alphabetical order but I am having problems. Firstly the code that I have used to sort the array may not even do what I need but havn't got far enough to test it yet so go easy on me . I have read in some places when searching how to do this that I would have to create my own bubble sort in order to achieve this but I was hoping that Java had a built in sort method/function. Secondly I lack the knowledge in java to be able to assign an existing array or even a variable to the newly sorted array as I need the unsorted version with the original name and the newly sorted version as another.

code (This is not all of the code, I decided to include only what I thought was relevant):

import java.util.Arrays;
public class Sentence {
private String words[];
public Sentence(String[] words) { this.words = words; }
@Override
public String toString() {
return "Sentence{" +
"words=" + Arrays.toString(words) +

[Code] ....

Is it possible to shorten the sort function to just this?

public String sorted() {
return Arrays.sort(words);
}

View Replies View Related

Scan Text And Add Each Word Alphabetical Listing?

Mar 4, 2014

I'm scanning my text and I want to add them into array list, but it is not easy. I want each word store from arrayList of a to z which is arrayList size is 26. Example:

arrayList a store string array of: an, and, apple, ...

arrayList b store string array of: be, become, became.....
.
arrayList z store string array of: zero, zone...

In my class, I create:

private static ArrayList<String[]> words = new ArrayList<String[]>(26); // to store all words

In main, I do while loop to get each words and store in words array,
 
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class BinaryTree {

[code]...

View Replies View Related

Sort Strings In Alphabetical Order - Method Not Being Applied

Jan 21, 2014

I'm doing an exercise we're you're supposed to sort strings in alphabetical order, without importing anything , not using the Arrays.sort() method.

I think I got the method down partially right, or it is on the right track, but it is completely not being applied to my answer. All it prints out in the console is the actual String array twice, without sorting anything.

public class arrayofstrings {
public static void sort(String[] a) {
String temp= "";
int min;
int i= 0;
for (int j=0; j<a.length-1; j++) {

[Code] ....

View Replies View Related

Reading Contents Of Several TXT Files - Output File Is Blank

May 4, 2015

In the program below I'm trying to read the contents of several .txt files in the same diretory and create a new .txt file containing all of the data from each file. My output is generated in the console however my .text file is blank.

public static void main(String[] args) throws IOException {
  String target_dir = "C:Files";
  String output = "C:Filesoutput.txt";
  File dir = new File(target_dir);
  File[] files = dir.listFiles();
 
[Code] .....

View Replies View Related

Sort User Input 3 Words Alphabetically And Output Middle Word

Feb 8, 2014

I have a project requiring me to build a program having a user input 3 words, sort them alphabetically and output the middle word. I have done some searching and seem to only come back with results for sorting 2 words. I so far have code to get the user input but I am completely lost as to how to sort them alphabetically.

import java.util.Scanner; //The Scanner is in the java.util package.
public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.
String str1, str2, str3;
System.out.println("Please enter three word words : "); //Prompt user to enter the three words

[Code]...

we havnt done arrays yet and I THINK i have to do compareTo.....how to use it?

View Replies View Related

How To Make File Reader Object Move To Next Line If There Is No More Input On Line

Feb 18, 2015

How do I make the file reader object move to the next line if there is no more input on the line. Here is my text and output file as you can see that my text file column cuts off on the 2nd line after 70. I want to read that next line which is 100 into my labs variable however its reading it into my final exams variable. I'll also post the code but I didn't think it was necessary.

textfile:
100908095
1008070
10070

output:
Labs Projects Tests Final Exams
100908095
1008070100
70

[import java.util.Scanner;
import java.io.*;
public class MyGrades
{
public static void main (String[] args) throws IOException
{
int lab, project, test;
int finalExam;//Par and Player values

[Code]...

not sure if I code wrapped it correctly

View Replies View Related

Program To Count Character Word And Line From A File?

Dec 27, 2014

public static void main(String[] args) throws FileNotFoundException, IOException{
// TODO code application logic here
FileReader fr = new FileReader("gautam.txt");
BufferedReader br = new BufferedReader(fr);
  int line = 0;
int word = 0;
int character = 0;
String s;
while((s=br.readLine())!=null)

[code]...

how to count the character from file

View Replies View Related

Read Line From File And Save First String As Word And Remaining As Meaning

Jul 6, 2014

I have written the code to read the line from file and save first string as word and and remaining string as meaning..

E.g.: innovation a new method, idea, product, etc.

word = innovation

meaning = a new method, idea, product, etc.

In my code there is no error and have a mistake what it is means first assigning word is ok and while saving meaning ..it saves like

{
a a new a new method } like that

Java Code:

import java.io.BufferedReader;
import java.io.FileReader;
public class Dil{
public static void main(String[] arg)throws Exception
{
BufferedReader in;

[Code] ......

View Replies View Related

Pickup Selected Text File And Read Line By Line And Output Text Into Visual Text Pane

Dec 12, 2014

I am checking how to do following task.

01. pickup the selected text file and read the line by line and output the text in to visual text pane.

what i did:.

01. I wrote code that read the text file and output in to jave console/ also some of the interface.

the code read txt file:

Java Code:

String fileName = "C:/Users/lakshan/Desktop/lawyer.txt";
File textFile = new File(fileName);
Scanner in = new Scanner (textFile);
while(in.hasNextLine()){

[code]....

so it will read any text file dynamically and output to the text pane in interface. I think scanner code must be execute after the select the file from the browser and set the scanned result in to variable. then later out put the var as string in some jswing component?

View Replies View Related

File I/O - How To Process Contents From Input File

Mar 22, 2014

So my assignment is to read these values from an input file which I previously created. The 10 values represent miles traveled and I convert them using some formula my prof gave me to get the total cost, and output both values to an output file.

My programming question is the first number on the input file isnt a mileage value its the number 10, which is the number of values to be processed, which we're supposed to use in order to control a for or while loop which we'll use to process the contents of the input file. How would I create a while or for loop and get it to skip the first value of the input file?

We're not allowed to use arrays for this assignment.

Also here's my code so far, and it says its not being able to find the file, I made sure the file name is exactly as saved, and its saved in the same directory as the java class that i created for this assignment so I don't see what the problem could be?

Here's my the class with the main method

import java.util.*;
import java.io.*;
 public class QudratullahMommandi_3_07 {
 public static void main (String[]args)

[Code] ....

This is the error message I get

----jGRASP exec: javac -g QudratullahMommandi_3_07.java

QudratullahMommandi_3_07.java:17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner input = new Scanner(inputSource);
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

View Replies View Related

Read A File And Then Output Back To Same File On New Line

Jan 24, 2015

I am trying to write to a file but everything I have tried doesn't seem to work. What I want to do is read a file and then output back to the same file on a new line. The text file contains the sentence "Java is a programming language".

import java.util.Scanner;
import java.io.*;
class HW1B {
public static void main(String[] args) {
Scanner fileIn = null;
try {
fileIn = new Scanner (new FileInputStream("text.txt"));

[Code] ....

These are the errors :

HW1B.java:19: error: no suitable constructor found for Scanner(FileOutputStream)
fileOut = new Scanner (new FileOutputStream("text.txt"));
^
constructor Scanner.Scanner(Readable) is not applicable
(argument mismatch; FileOutputStream cannot be converted to Readable)
constructor Scanner.Scanner(InputStream) is not applicable

[Code] ....

Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors

View Replies View Related

Searching TXT File For A Specific Keyword And Output Whole Line

Feb 12, 2014

I need to search a txt file for a specific keyword and then output all the lines that contain that keyword. Right now I I think I have my search done but I don't know how I would print the whole line.

TextIO.readFile("xxx.txt");
String search;
String word;
int count=0;
TextIO.put("Please enter your search word: ");
search = TextIO.getln();
while (!TextIO.eof()) {
word = TextIO.getln();
count = count+1;
if (search.equalsIgnoreCase(word)==true){
TextIO.put(count + "-");
TextIO.put(word);

Right now it doesnt even let me enter in any values for the search. Not sure what I've done wrong..

View Replies View Related

Read File And Input Each Line Of File Into Array

Mar 21, 2015

I am trying to read a file and input each line of the file into an array. I have not set the array size as I was hoping to fill the array using a while loop. Unfortunately the scope of the array does to work inside the while loop so I am being told that the array 'students' has not been initialised even though it has just outside of the while loop. Is it possible to do what I am trying to without having the array initialised in the while loop as surely the array will be reset every time if it was in the while loop? Here is my code:

public static void students(String file) throws FileNotFoundException {
try {
File studentInfo = new File(file);
Scanner input = new Scanner(studentInfo);
String[] students;

[Code] ....

View Replies View Related

Read Input File And Create Output With Anagram For Words In File

Sep 24, 2014

Well my code is supposed to ask for an input file and then (ex: input.txt), read the input file and create an output.txt file with the anagram for the words in the file. Also it should be displayed on the screen. However my code doesn't display the anagram on screen or the output file!

Heres is the code:

import java.io.*;
import java.lang.*;
import java.util.*;

/* This program will read a file given by the user, read the words within the file and determine anagrams of the given words. If the file that the user inputs is empty, then the program will output "The input file is empty."
* The program will read the file line by line, counting the total number of words read. If there are more than 50 words, "There are more than 50 words."
* will be printed, and the program will terminate. After each line is read, the words in the line will be separated,punctuation characters will be removed, and upper case characters will be switched to lower case.
* If any word is larger than 12 characters, that word will not be considered in the total amount of words in the file and it will not be sorted.
* After each word is read, the letters will be sorted and stored into an array containing each
* word's 'signature'. After all the words have been read, words will be printed to the output file on the same line based upon their signature.
*/

public class Anagram {
//Creating constants for maximum words in file and maximum chars in word
public static final int MAX_CHARS = 12;
public static final int MAX_WORDS = 50;

[Code] ....

View Replies View Related

Floating Point Numbers - Print Contents Of Array In Reverse Order

Feb 27, 2014

I am trying to do this assignment but I can't get the needed output.

Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

Program is written to a class called ReverseNumbers.

Example output

How many floating point numbers do you want to type: 5

Type in 1. number: 5,4
Type in 2. number: 6
Type in 3. number: 7,2
Type in 4. number: -5
Type in 5. number: 2

Given numbers in reverse order:

2.0
-5.0
7.2
6.0
5.4

Java Code:

import java.util.Scanner;
public class apples {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double[] numbers;

[Code] .....

View Replies View Related

Read Line Of Input For A File - String Tokenizer

Jan 27, 2015

So I'm having a problem with the .hasMoreElement() method. I try to read lineOfInput for a file which contains:

Hobbit, Long,t@gmail.com,475-555-4444,3
Long, Hobbit,b@yahoo.com,445-222-5342,2
Hobbit,Long,b@yahoo.com,465-222-5342,2
Help,Yerp,,455-222-2222,4

but when i get to the ,, on the last line I get a NoSuchElementException and the program crashes.

StringTokenizer inputField = new StringTokenizer(lineOfInput, ",");
while(inputField.hasMoreElements()) //for each fields is a line
{
lastName = inputField.nextElement().toString();
firstName = inputField.nextElement().toString();
email = inputField.nextElement().toString();
phoneNumber = inputField.nextElement().toString();
level = Integer.parseInt(inputField.nextElement().toString());
}

View Replies View Related

Total Number Of Each Alphabetical Letter From A File

Nov 28, 2014

So far I've got it where the program is reading the file and returning

a:0
b:0
c:0
d:0
e:0
f:0
g:0
h:0
i:0
j:0
k:0
l:0
m:0
n:0
o:0
p:0
q:0
r:0
s:0
t:0
u:0
v:0
w:0
x:0
y:0
z:0

But I need it to print the letters and then the total amount of each character that is found in the file.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

[code]....

View Replies View Related

How To Use File Input / Output In Addition To Exception Handling

Dec 2, 2014

I am trying to learn how to use file input/output in addition to exception handling... The problem is my textbook wrote this chapter for a version of Java that hasn't come out yet, so everything I do "according to the textbook" doesn't work. any feedback on correcting these exception errors because I am not sure what is causing them or how to fix them.

I was able to have it display the name of the book in the Book.txt file, but when I added the second part if the file doesn't exist, that's when the errors came up and it wouldn't compile.

import java.io.*;
import java.util.*;
public class DisplayBook
{
public static void main(String[] args) {
try {
File book = new File("Book.txt");
FileInputStream in = new FileInputStream(book);

[Code]...

These are the compilation error messages I am receiving: (I have managed to get it down from 7 errors to just 4, but now I'm stuck)

DisplayBook.java:15: error: unreported exception IOException; must be caught or declared to be thrown
while ((letter = in.read()) != -1) //if file exists, displays book title
^
DisplayBook.java:24: error: unreported exception FileNotFoundException; must be caught or declared to be thrown

[Code] ....

4 errors

View Replies View Related

Regex To Filter Any Word In Any Order In Column Of JTable?

Jul 28, 2014

I created filters for every column in my Jtable however, some of these columns have more than one word inside of them and my filters will only filter them based on the first word. For example if I had a first and last name in one column, it will filter the table if I enter the first name in my filter text field but it will not filter that same column if I only input the last name. What is a good regex expression to filter any word in any order?

public static void filterRows() {
String filterId = idFilter.getText();
String filterFrom = fromFilter.getText();
String filterTo = toFilter.getText();
String filterCC = ccFilter.getText();
String filterDate = dateFilter.getText();
String filterSubject = subjectFilter.getText();

[Code]...

View Replies View Related

Scanning Next Line Of Input Without Advancing Past Line

Apr 12, 2014

I'm doing an assignment for uni and have come across a small hiccup. What I'm trying to do is scan in a text file and read "commands" for it line by line, E.g:

Student Mary 12345 19
Student Joe 12346 19
Change Joe 19 20
Change Mary 19 20

So that lines that begin with the word "Student" indicate that I should create a new student file with that name, student ID, and age.

"Change" indicates that I should be changing the specified student's current age to the new age etc.

What I'm currently doing is something along the lines of this:

Scanner input = new Scanner(new FileReader(args[0]));
String[] line;
while (input.nextLine().startsWith("Student")) {
line = input.nextLine().split("s+");

[Code] ....

The problem I'm having is that every second line seems to be getting skipped (because I'm calling nextLine() so much?) but I can't think of a way to "peek" at the first word of each line without advancing past it. Is there any way of doing this?

View Replies View Related

Reading Text File With Specific Input Format - Output Formatted Report

Apr 10, 2014

Here's a link to it : [URL] ....

The basic gist is it's "A program that reads in a text file that uses a specific input format and uses it to produce a formatted report for output."

Specifically :"For this lab you will write a Java program that produces a simple formatted report. The program will prompt the user to enter a file name. This file must contain information in a specific format (detailed below). Each "block" of the file contains information for one player in a competition -- the name of the player followed by a number of different scores that that player achieved. The program should find each player's average score, median score and best and worst scores and display them in a line on the final summary report. The program should also determine which player has the highest average score and which player has the lowest average score."

I get the following errors when I try and compile it:

Enter an input file name: Project11.txt
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException... -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Project11.getMedian(Project11.java:68)
at Project11.main(Project11.java:27)

I get that the error(s) reside in lines 68 and 27, among problem other areas, but I'm not exactly sure how I can fix them.

Here's my code:

import java.io.*;
import java.util.*;
public class Project11 {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.print("Enter an input file name: ");
String input = in.nextLine();

[Code] ....

View Replies View Related

AutoCorrect / Replace A Word That Is Similar To A Word In File Text

Mar 13, 2015

The intentions of this program is to prompt a user to enter a file name, and then reads the file. The program will prompt the user to enter a word that needs to be corrected. So lets say I have a text file containing "My name is OP and I Like goind to the Park!" I want to change "goind" to "going",

Now, my second method "isSimilar" executes a similar word with more than one same letter and same length, but I dont know how to execute that whole thing in my third method "correctThisLine" . How I can call that isSimilar method and read in that text file and change that word into that?

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WahidMuhammadA3Q2{
String fileName = "AutoCorrectMe.txt";
public static void main (String [] args){

[Code] ....

View Replies View Related

Write A Program To Reverse Each Word In Line (sentence)

Jul 3, 2014

Write a program to reverse each word in line(sentence)?

View Replies View Related

Reverse Order Array Output

Oct 10, 2014

I'm trying to make this code's output display like a sentence, since right now it displays downward and doesn' look right.

public class ReverseOrder {
public static void main(String[] args) {
String phrase = "The rain in Spain falls mainly on the Plain";
char[] phraseArray;
phraseArray = phrase.toCharArray();
for(int i = phraseArray.length - 1; i >= 0; i--){
System.out.println(phraseArray[i]);
}
} // end main
} // end ReverseOrder class

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved