Sorting A Text File With Strings And Numbers

Oct 16, 2014

how to sort my text file. So far I have been able to read the text file and print it back out, but I am unsure of how to go about sorting it. Must print the colors (in the order of the rainbow first) and if the colors are the same compare the size (bigger is more important)The values I have to sort are written as such in the text file:

blue 18
blue 10
red 27
yellow 4

public class Rainbow{
private String color;
private int size;
public Rainbow(String color, int size){
this.color = color;
this.size = size;

[code]....

I would know how to sort it if it was supposed to be alphabetical order or there were only numbers, but I can't seem to figure out how to sort it when there are strings and integers

View Replies


ADVERTISEMENT

Sorting Lowercase Strings Before Uppercase Strings

Mar 26, 2014

I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.

View Replies View Related

Sorting A Text File Alphabetically

Oct 13, 2014

So we have to create a code that reads in a file that has a huge list of words. Our code has to convert every word to all lower cases and then re-sort alphabetically. I was able to turn everything to lower case, but for the re-sorting I keep getting a null pointer exception on the line of the if statement.

import java.io.*;
public class readDict {
public static void main(String args[]) {
try {
System.out.println("Opening the file...");

[Code] ....

View Replies View Related

Separating Letters In Text File To Two Different Strings?

May 13, 2013

I have a text file that has the results of rock paper scissors game.

R P
S R
P P

I need to have the first letter in each line assigned to player1 and the second letter in each line assigned to player 2. where to start. I have the file input correct. It is reading the file and I can separate ints from strings. I just don't know how to separate the strings.

View Replies View Related

Assigning Empty Score To Strings In Text File

May 27, 2014

I have a txtfile which I read and go through. My question is what are the ways I could do to read a txtfile of words and assign an empty score to each of the word in it. So each word will have a 0 value. Later on I will manipulate the score but for now I want each word to have a 0 score.What I have at the moment is reading a text file full of words eg:

private void readtextfile() {
try {
Scanner rd = new Scanner(
new File("filename"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());

[code]...

how to make each of the words to have a score of zero 0?

View Replies View Related

Reading User-specified Text File Into ArrayList Of Strings Using Java?

Apr 23, 2015

I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:

Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);

View Replies View Related

Sorting Array Of Objects With Strings?

Feb 14, 2015

An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.

Java Code: public static Exam[] collateExams(Exam[] exams) {
Exam [] r = new Exam[10];
r = exams;
int[] position = new int[10];
int index = 0;
for(int i = 0; i < exams.length; i++)

[Code]...

View Replies View Related

Sorting Array Of Strings From User Input

Apr 3, 2015

I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.

I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work.

import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements to be sorted: ");
String element_number = input.nextLine();

[Code] ....

View Replies View Related

How To Read Numbers And Names From A Text File

Mar 20, 2014

I have a text file containing numbers and names as shown in the example below:

61133128805241Albert Rosenberg
64763645543509Joel
79256337754219Michael Burton

I'm making a program that will read the file and put the numbers in a list of int arrays and names in another list of strings. In my program i created two classes. One will receive the numbers and the other will receive the names. But i only can read the numbers! How can I read everything and separate into two different lists?

try( BufferedReader br = new BufferedReader( new FileReader( jFileChooser1.getSelectedFile().getAbsolutePath() ) ) ) {
hist = new Historic();//Will recieve the array of numbers.
while( br.ready() ) {
int line[] = new int[ 7 ];

[Code] ...

View Replies View Related

Counting The Frequency Of Numbers Inside A Text File

Apr 7, 2014

I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).

import java.io.*;
 
public class letterfrequency {
public static void main (String [] args) throws IOException {
File file1 = new File ("letternumberfrequency.txt");
BufferedReader in = new BufferedReader (new FileReader (file1));
 
[Code] ....

But, let's just say that now I have the following characters in the text file, "letternumberfrequency.txt":
71 geese - 83 cars - 58 cows- 64 mooses- 100 ants- 69 bangles- 90 molehills - 87 noses

The numbers inside that text file would be considered as strings, am I right? But I want to extract the numbers so that I can also be able to count their frequency - not as individual digits but as whole numbers (that is how many "71", "83", "58", "64", etc. are there...). Would using "Double.parseDouble ()" work?

View Replies View Related

Numbers Inputted By User And Sorting Them Out In Ascending Order

Nov 12, 2014

I am writing a program that is supposed to take 3 numbers inputted by the user and sorting them out in an ascending order. I believe my code is correct but can't figure out why the program isn't behaving as expected.

import java.util.*; //Required to use the scanner console
public class Week3_Programming_Problem
{
static Scanner console = new Scanner(System.in); //Allows for user input

public static void main(String[] args)

[code]....

View Replies View Related

Lottery - Sorting Numbers From Smallest To Biggest Without Repetition

Oct 29, 2014

I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.

package lottonumbers;

public class LottoNumbers {
public static void main(String[] args) {
int[] lottery = new int[6];
for (int numoftimes=0;numoftimes<5;numoftimes++){

[Code] ....

View Replies View Related

How To Concatenate Strings Of Binary Numbers Together

May 23, 2015

Ok so what I a String array of binary numbers

private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

and what I want to do is simply run through a loop (Which I already made ) that will randomly just put this strings together.
So if the loop runs once it will return something similar to

00001011001101000110

The thing is that I don't want to return such a number as an String, I want it to be returned as a long.This was one of my recent attempts

/* Contains the genes or possible solutions to the problem
*/
public class Genes
{
/* Each element is a binary number that corresponds to index number they have been assigned to, these are the possible genes
* The last 4 elements in the array represent + - * / correspondingly
*/
private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

[code]...

What can I do ? I want them the values returned to be of the long data type, but I don't want the binary numbers to be added together. I just simply want them to be placed one next to each other at random patterns

View Replies View Related

Recursion - Converting Numbers To Strings

Jul 7, 2014

I found an open-source recipe for converting numbers to words.

I modified it a bit since the requirement is only for integers until 999:

public class NumberToWordsConverter {
private String[] ones = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};

[Code] .....

From robosoul's response at StackOverflow, I simply inserted a condition for negative integers (line 12). True enough, the code worked and I was able to trace (pen and paper method) how it is converting the 0 and positive integers to words.

However, I am a bit lost on how it is doing the conversion for negative integers. How it is successfully converting the negative?

View Replies View Related

Need To Count Strings That How Many Inputs From User Were Numbers

Jan 30, 2014

import java.util.Scanner;
public class CountStringNumbers { // show the number of Strings that were numbers . . ..
public static void main(String []args){
int count = 0;
int countNumbers=0;
Scanner input=new Scanner(System.in);

[code]...

can count the numbers from 0-9 if the user puted but if it is 10 it doesnt count it , and i need to write all the numbers to have that option. . . . Is there an easy way that includes all the numbers? like if(word.equals(number)){<<<???? (numbers) i know htat doesnt work but is there anything similar ?? ??

countNumbers++;
}
System.out.println("Type Something , , , and to end the the termination type STOP");
word=input.nextLine();
count++;
}
System.out.println("You wrote "+count+" lines wich from those lines "+countNumbers+" were numbers and the programe terminated ");
}
}

View Replies View Related

Place Leading Zeros Onto Numbers Without Having To Convert Them To Strings

Jan 21, 2014

Any way to place leading zeros onto numbers without having to convert them to strings? Is such a thing possible?

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Convert Strings To Double Numbers - Exception When Non-numeric Entered

Nov 11, 2014

So I have to convert strings to double numbers and there can be no exception.

The strings that aren't numbers or do not fit into a set criteria have to be discarded.

When I try to write this I get an exception when a non-numeric is entered and the code stops.

What can I do? Also, am I finding the average of the array correctly?

import java.util.*;
public class Grades{
public static void main(String args[]){
int arraycount = 0;
final int SIZE = 10;
int validArraycount = 0;
final int ValidArraySize = 10;

[Code] ......

View Replies View Related

How To Make A Text File Only Use Lines In A Text File Beginning With A Certain Letter

Mar 15, 2015

I have a program that reads lines of text, but some of the lines of text aren't applicable and break the program. I'd like to put a letter in front of the lines in the .txt file I want to use, such as a #.

I need to make an if loop that'll check for the first letter on the line being #, and use the line in the program if true and skip if false. I'm guessing a boolean variable would be useful here to be true or false depending on the presence of #, but I don't know how to only read the first letter of each line, how can I do this?

View Replies View Related

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000

public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);

[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

Reading / Sorting And Writing A File

Dec 4, 2014

I need to read lastname, firstname, and ssn from a text file and sort them based on oneof those three creiteria. Here is my person class:

public class Person implements Comparable {
private String lastName, firstName, ssn;
private static int sortBy = 1;
public static final int LASTNAME=1, FIRSTNAME=2, SSN=3;
public void setSortMethod(int method) {
if(method != LASTNAME && method != FIRSTNAME && method != SSN) {
throw new IllegalArgumentException();
} else {

[code]....

how to sort my info and store it into an array and then write that info into my new file.

View Replies View Related

Sorting Data Alphabetically By Second Name In A File?

Apr 11, 2013

I have a problem sorting this file alphabetically by second name. Basically, my method sorts each column alphabetically but i would like to sort the file according to the second name.

File:
Moe     Carl
Saul     Sergio
Rocky     Louis
Ike     Ziken     

This is how my method sorts the file:

Ike          Carl
Moe          Louis
Rocky          Sergio
Saul          ziken
     
Instead
Moe     Carl
Rocky      Louis
Saul     Sergio
Ike     Ziken

Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class thedata implements Comparable
     {               
          private String firstname;
          private String secondname;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Sorting In File Tree Node

May 12, 2014

I have requirement of sorting the files in tree structure with name and timestamp of creation, i am new to swings...

Here we are using FileTreeModel.

Root Folder:
folder1;
file1
file2
.
.

View Replies View Related

Reading A File / Sorting It And Displaying Its Content On Screen

Jan 19, 2014

I have a program that writes a user's input to a file.

The input consists of several loops. In each loop the user enters a student number, the student first name and last name and then several exam results. Each student has different number of exams, depend on the user input.

For instance, the input can be (explanations are in brackets but not in the actual file):

505 (student number)
efron (last name)
ben (first name)
3 (no. of exams)
88 (1st exam mark)
70 (2nd exam mark)

... and so on

I need to write a program that reads that file and writes it to the screen, but it needs to do it in ascending order according to the student number.

Leaving aside the format it will be displayed on the screen, my question is how should I sort the data by the student number?

I though about first reading each student as an array, but my problem is that each array contains different number of elements (depends on the number of exams) and also each array containד both strings (first and last names) and ints.

View Replies View Related







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