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
Related
So basically I'm suppose to take the numbers from a file Ex.
And turn it into this Ex.
We're suppose to make a graph class and store a adjacency list representation of a graph. We're also suppose to do it with an array of arraylists. So I got some help making the graph class and I'm making it so that the users file is processed through the graph class but for some reason there's an error and the output isn't right. Can someone help with this?
Graph Class
import java.util.ArrayList;
public class Graph {
ArrayList<Integer> [] nodes;
int n_nodes;
public Graph(int numberNodes){
this.nodes = new ArrayList[numberNodes+1];
this.n_nodes = numberNodes;
for(int i = 0; i < n_nodes + 1; i++){
nodes[i] = new ArrayList<>();
}
}
public void addNeighbor(int node, int neighbor){
nodes[node].add(neighbor);
}
public String toString(){
StringBuilder myGraph = new StringBuilder();
for(int i = 1; i < nodes.length; i++){
myGraph.append(i);
ArrayList<Integer> neighbors = nodes[i];
int totalNeighbors = neighbors.size();
for(int j = 0; j < totalNeighbors; j++){
int myneighbor = neighbors.get(j);
myGraph.append(" -> " + myneighbor);
}
myGraph.append('\n');
}
return myGraph.toString();
}}
Main Class
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) throws IOException {
File file = null;
JFileChooser chooser = new JFileChooser();
Scanner ans = new Scanner(System.in);
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
} else {
JOptionPane.showMessageDialog(null, "No File Selected");
System.exit(1);
}
Scanner input = new Scanner(file);
int y = input.nextInt();
int x = 0;
Graph graph = new Graph(y);
while (input.hasNextLine()) {
for (int i = 0; i < y; i++) {
x = input.nextInt();
graph.addNeighbor(i, x);
}
System.out.println(graph.toString());
}
}}
Also I'm new to the whole stackoverflow website so sorry if my wording isn't clear or my code isn't formatted good enough.
Edit
This is the error it's showing
As 3 as read by
int y = input.nextInt();
is on a different line then you need to read the CR-LF as well.
Personally I would use the following paradigm
str = input.nextLine ()
// convert to int
while (input.hasNextLine) {
str = input.nextLine ()
arr[] = str.split (" ");
// loop through length of arr - in your code `y` is not updated
add arr[x] to graph
so basically you want something like
Scanner input = new Scanner(file);
String line = input.nextLine();
int y = 0;
if (line != null) {
y = Integer.parseInt(line.trim());
}
Graph graph = new Graph(y);
while (input.hasNextLine() && y > 0) {
if (input.hasNextLine()) {
line = input.nextLine();
}
String nums[] = line.split(" ");
for (int i = 0; i < nums.length; i++) {
int x = Integer.parseInt(nums[i]);
graph.addNeighbor(i, x);
}
System.out.println(graph.toString());
}
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();
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);
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.
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(...).