Create a random txt file of approximate size in Java - java

OK, I have found the following code somewhere that generate a random txt file. Basically I want random words separated by some whitespace in order to run MapReduce word counting simulations.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
PrintWriter writer = new PrintWriter("bigfile.txt", "UTF-8");
Random random = new Random();
for(int i = 0; i < 23695522; i++)
{
char[] word = new char[random.nextInt(8)+3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
for(int j = 0; j < word.length; j++)
{
word[j] = (char)('a' + random.nextInt(26));
}
writer.print(new String(word) + ' ');
if (i % 10 == 0){
writer.println();
}
}
writer.close();
} catch (IOException e) {
// do something
}
}
}
Now I want to alter this code a bit in order to have as much iterations as needed for the file to have approximately a predefined size. So, every iteration produces about 6.5 characters (due to uniform selection) each of 2 bytes. So, I divide the size of file I want in bytes by (6.5*2), set the result as the number of for loop iteration and get a file much smaller than I expect it to be.

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
long count=0;
try{
File file = new File("bigfile.txt");
PrintWriter writer = new PrintWriter(file, "UTF-8");
Random random = new Random();
for(int i = 0; i < 23695522; i++)
{
char[] word = new char[random.nextInt(8)+3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
count+=word.length;
for(int j = 0; j < word.length; j++)
{
word[j] = (char)('a' + random.nextInt(26));
}
writer.print(new String(word) + ' ');
count+=1;
if (i % 10 == 0){
writer.println();
count+=2;
}
}
writer.close();
} catch (IOException e) {
// do something
}
System.out.println(count);
}
}
Try this one. Newline char is 2 byte and the others are 1 byte.

how about counting bytes and loop until you get the right amount of bytes?
int writtenBytes = 0;
do{
String randomWords = ....;
writtenBytes += randomWords.getBytes(StandardCharsets.UTF_8).length;
writer.print(randomWords);
}while(writtenBytes < 123456);

Related

How to save a deleted character with stringbuilder

Is it possible to save a character which was deleted with the Stringbuffer or delete all character except one on 'i' index
This is my input from a txt file:
3;8;4;5;3;2
3 4 5 1 2 3
9;8;3;2;3;4
9 8 9 7 8 1
I need to sum up each line and see in which one is most of even numbers, so i decided to read a whole line, separate characters with a space then with help of string builder delete all characters except one on 'i' position and finally save in two dimentional array.
Maybe you'll have some better idea how to do it?
My code:
package Operacje_na_plikach;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Zad3 {
/*
Plik tekstowy ‘dane.txt’ ma postać:
3;8;4;5;3;2
3 4 5 1 2 3
9;8;3;2;3;4
9 8 9 7 8 1
Pobierz z pliku tekstowego kolejne wiersze liczb i wypisz na ekranie numer wiersza, w którym występuje najwięcej elementów parzystych.
*/
public static String[][] odczyt(String nazwa)
{
String[][] arr = new String[1][1];
int[] suma = new int[1];
int max = -1;
int wiersz=-1;
String text = null;
try {
FileReader reader = new FileReader(nazwa);
Scanner sc = new Scanner(reader);
while(sc.hasNextLine())
{
arr=Arrays.copyOf(arr,arr.length+1);
text = sc.nextLine().replaceAll(";"," ");
int[] temp= new int[text.length()];
StringBuilder sb = new StringBuilder(text);
for (int i = 0; i <temp.length ; i++) {
temp[i]=sb.delete();
}
for (int i = 0; i < ; i++) {
for (int j = 0; j < ; j++) {
arr[i][j] = text
}
}
}
/* while (sc.hasNextLine())
{
arr=Arrays.copyOf(arr,arr.length+1);
text = sc.nextLine().replaceAll("[;]"," ");
for (int i = 0; i <arr.length ; i++) {
while(text!=null)
{
int temp = Integer.parseInt(text);
}
for (int j = 0; j <arr.length ; j++) {
arr[i][j] = text.nextInt();
if(arr[i][j]%2==0)
{
suma[i]+=arr[i][j];
if(suma[i]>max)
{
max = suma[i];
wiersz=i;
}
}
}
}
}
System.out.println("Najwiecej liczb parzystych jest w wierszu: " + wiersz);
*/
sc.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return arr;
}
public static void main(String[] args) {
int[][] arr = odczyt("dane.txt");
}
}
I think you may be over-complicating things. You could stream the lines of the file, parse them, and map them to the sum of the even numbers. You can then go over the array of sums and find the index of the largest element (which can also be done with streams, just for the fun of it):
long[] sums =
Files.lines(Paths.get("dane.txt"))
.mapToLong(s -> Arrays.stream(s.split("[ ;]"))
.mapToInt(Integer::parseInt)
.filter(i -> i % 2 == 0)
.sum())
.toArray();
int lineNum =
IntStream.range(0, sums.length)
.boxed()
.max(Comparator.comparingLong(i -> sums[i]))
.get();

How to read integers from a text file without using hasNextInt() Java

So I'm new to programming and we have to do this lab where we read from a text file that has only one line with numbers. We then have to put those integers into an array. I know how to read the numbers when they are in separate lines but not when they are in one line. I also know how to put numbers into an array, so I don't need help with that.
The only methods we are allowed to use are:
hasMoreTokens()
hasMoreLines()
readDouble()
readInt()
readLine()
readToken()
Is there a way to do such while using only these methods?
Here is the code:
import chn.util.FileInput;
import chn.util.FileOutput;
public class Compact {
public Compact (FileInput inFile, FileOutput outFile){
int[] compactArray = new int [21];
int numZeroes = 0;
int num;
int length = compactArray.length;
for (int i = 0; i < length; i++) { //making the array for the integers in the list
compactArray[i] = 0;
}
while (inFile.hasMoreLines()) {
num = 0;
num = inFile.readInt(); //reads the integers per line
compactArray[num]++;
}
for(int i = 0; i < length; i++) {
if(compactArray[i] == 0) {
length--;
for(int j = i; j < length; j++) {
compactArray[j] = compactArray[j+1];
}
i--; // Decrement i to check the value that was shifted
} else {
numZeroes++;
}
}
// now print the array without 0
for(int i = 0; i < numZeroes; i++) {
outFile.print(" " + compactArray[i]);
}
outFile.close();
}
For some reason it's simply returning a string of zeroes. I was thinking it may have to do with the way I read the code.
Input: numbers_line.txt which has one line:
1 8 3 43 4 56
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class ReadIntFromFile {
public static void main(String []args){
String fileName = "numbers_line.txt";
List<String> numbersArrayList = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
numbersArrayList = Arrays.asList(line.split(" "));
}
} catch (IOException e) {
e.printStackTrace();
}
String[] numbersStringArray = new String[numbersArrayList.size()];
numbersStringArray = numbersArrayList.toArray(numbersStringArray);
int[] numbersIntArray = new int[numbersStringArray.length];
for(int i = 0;i < numbersStringArray.length;i++) {
numbersIntArray[i] = Integer.parseInt(numbersStringArray[i]);
}
for(int x : numbersIntArray)
System.out.println(x);
}
}
Output:
1
8
3
43
4
56

Trying to insert text from file to 2D array. Java. Text is different formats

So I have a text file containing ints, doubles, strings that I need to read into a 2D array. there Array should have 6 columns but the rows are not known until you read the entire file. I'm guessing it is some 700 rows This is what I have so far. If I eliminate the array it prints fine but with the array I keep getting errors.
I have searched many questions like this but they usually only work with ints/doubles. Also please don't recommend arrayList as that has not been taught in our course.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class TitanicApp {
public static void main(String[] args) throws FileNotFoundException {
String[][] array=null;
int i=0, j=0;
String fileLine;
String s;
Scanner scannerIn=null;
BufferedReader inputStream=null;
try{
inputStream = new BufferedReader(new FileReader("titanic.txt"));
scannerIn = new Scanner(inputStream);
while ((fileLine = inputStream.readLine()) != null){
for (j=0;j<6;j++){
array [i][j]=scannerIn.next();
System.out.println(array[i][j]);
i++;
}
}
}
catch (IOException ex){
ex.printStackTrace();
}
}
Sample text in file :
1 1 Allen, Miss. Elisabeth Walton female 29 211.3375
1 1 Allison, Master. Hudson Trevor male 0.9167 151.5500
And the output should be :
[ [1, 1, Allen, Miss. Elisabeth Walton, female, 29, 211.3375]
[1, 1, Allison, Master. Hudson Trevor, male, 0.9167, 151.5500]
]
I have a txt file containing the above info (and more lines) and I need to put it into a 2D array. I had to remove more than half of it since there was a limit.
So you want to do it using array only. You can consider below example as reference -
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TitanicApp {
int size = 10;
int colCount = 6;
String[][] array = null;
public static void main(String[] args) {
TitanicApp app = new TitanicApp();
app.startExec();
}
public void startExec() {
array = new String[size][colCount];
String fileLine;
int i=0;
try
{
BufferedReader inputStream = new BufferedReader(new FileReader("C:/titanic.txt"));
while ((fileLine = inputStream.readLine()) != null) {
addTo2DArray(fileLine.split("\\t"), ++i);
}
inputStream.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
// Now you have the desired array : array;
}
public void addTo2DArray(String[] tmpArray, int minCapacity) {
if((minCapacity > size)) {
size = (size * 3)/2 + 1;
String[][] newArray = new String[size][colCount];
for(int i=0; i<array.length; i++) {
for(int j=0; j<array[i].length; j++) {
newArray[i][j]=array[i][j];
}
}
array = newArray;
}
array[minCapacity-1] = tmpArray;
}
}
Another Solution :
String content = new Scanner(new File("/titanic.txt")).useDelimiter("\\Z").next();
String[] rows = content.split("\n");
String[][] finalArray = new String[rows.length][];
for(int i=0; i<rows.length; i++)
{
finalArray[i] = rows[i].split("\\t");
}
//Your desired array is : finalArray;
From what it looks like, you are incrementing both array locations, (i and j), at the same time. If you want the input to go in sequential order in the array increment j until array.size and then increment i. An example would be a nested for loop.
for(int i = 0; i < array.size; i++){
for(int j = 0; j < array.size; j++){
array[i][j] = scanner.next();
System.out.println(array[i][j]);
}
}
or in your case, stop incrementing the values until i and j get to 6 instead of array.size
I think you need declare a size for your String[][], otherwise you will get a NullPointerException. ArrayLists are the only way to dynamically generate the list as you move on as far as my knowledge goes.

Java program not outputting negative integers from array?

One of my Java assignments is to take numbers from a file and then seperate them to two arrays. One named P (positive numbers) and N (negative numbers.) I have it working for the positive numbers but the negative numbers keep outputting 0s. I have no idea why! Help?
import java.io.*;
import java.util.*;
public class Prog404a {
public static void main(String[] args) {
Scanner inFile = null;
try {
inFile = new Scanner(new File("prg404a1.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!!");
System.exit(0);
}
int temp = 0;
int P[] = new int[23];
int N[] = new int[23];
int i = 0;
while (inFile.hasNext()) {
temp = inFile.nextInt();
if (temp < 0) {
N[i] = temp;
}
if (temp > 0) {
P[i] = temp;
}
i++;
}
for (int x = 0; x < i; x++) {
System.out.println(P[x] + "\t" + N[x]);
}
}
}
EDIT: Never mind it's not working for positive numbers either. Only a few.
Maybe you are just not counting right?
You should be using two counters, one for positive, one for negative numbers.
Otherwise, half of the entries will obviously be 0, because they were never set.

Java Anagrams reading from dictionary file

I am having trouble comparing my dictionary file to the anagrams. I put a print statement at each and it is reading in the dictionary file correctly and it is also calculating all of the anagrams correctly But it won't calculate only the anagrams from the dictionary file. I'm pretty sure it's something very minor and if someone can fix it it would greatly be appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Anagram3
{
static int size;
static int count;
static char[] charArray;
static char[] words;
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Type the path of the dictionary to read from : ");
String fileName = sc.nextLine();
List<String> dictionary = new ArrayList<String>();
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(fileName));
String word;
while((word = br.readLine())!=null)
{
dictionary.add(word);
}
}
catch(IOException e)
{
e.printStackTrace();
}
String[] words = new String[dictionary.size()];
dictionary.toArray(words);
//for( int i = 0; i < words.length; i++ )
// System.out.println(words[i]);
System.out.println("\nEnter the phrase to scramble: ");
String input = sc.nextLine();
System.out.println();
size = input.length();
count = 0;
charArray = new char[size];
for (int j = 0; j < size; j++)
charArray[j] = input.charAt(j);
doAnagram(size);
}
public static void doAnagram(int newSize)
{
int limit;
if (newSize == 1) // if too small, return;
return;
// for each position,
for (int i = 0; i < newSize; i++) {
doAnagram(newSize - 1); // anagram remaining
if (newSize == 2) // if innermost,
printAnagrams();
rotate(newSize); // rotate word
}
}
public static void rotate(int newSize)
{
int i;
int position = size - newSize;
char temp = charArray[position];
for (i = position + 1; i < size; i++)
charArray[i - 1] = charArray[i];
charArray[i - 1] = temp;
}
public static void printAnagrams()
{
for (int i = 0; i < size; i++)
{
//System.out.print(charArray[i]);
if(charArray[i] == words[i])
{
System.out.print(charArray[i]);
}
}
System.out.println();
}
}
Your static variable words is not being used because you define a new String[] words before the assignment.
Use the equals method to compare Strings. 1
Another issue is that you're comparing the i'th anagram generated to the i'th element in your dictionary, when you actually (presumably) want to test if the i'th anagram is present in the dictionary at any position.
You can try using a HashSet h of strings, rather than the array, for the dictionary, and then check for validity of an anagram with h.contains(...).

Categories

Resources