Here is the code:
class Test {
public static void main (String[] args) throws Exception {
java.io.File fail = new java.io.File("C:/Users/Student/Desktop/Morze.txt");
java.util.Scanner sc = new java.util.Scanner(fail);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lst = line.split(" ");
int[] letter = new int[26];
int[] sumbol = new int[26];
for (int i = 0; i < lst.length; i++)
System.out.print(lst[i] + " ");
System.out.println();
// How to add?
}
}
}
Please, explain how can I add all letters into list Letter and symbols into list Sumbol?
Content of the file Morze.txt:
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
Thanks!
You don't have a list, you have an array(s). It appears you want to add the values to two arrays. However you appear to have some code in your loop which should not be in your loop.
Additionally your data is text/String not numbers/int values.
String[] letter = new String[26];
String[] symbol = new String[26];
int count = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lst = line.split(" ");
letter[count] = lst[0];
symbol[count] = lst[1];
count++;
}
for (int i = 0; i < count; i++)
System.out.println(letter[i] + " " + symbol[i]);
I'm going to offer a solution that fixes your implementation because I think it might help you understand a few concepts. However I would recommend once you get it working that you go back and read about the Java List interface and re-write your code. Lists are much cleaner way of maintaing sequences that may grow or shrink in length and will greatly reduce the complexity of your code.
You should start by moving your letter and symbol array declarations out of your while loop. Variables within a block in Java are scoped to its bounds. In other words, no statement outside the while loop has visibility of either array. This has the side-effect of creating a new array for every line you parse using your scanner.
int[] letter = new int[26];
int[] sumbol = new int[26];
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lst = line.split(" ");
Next you'll need to know where to put your current symbol/letter in the array, an index. So you'll want to keep a count of how many lines/symbols you've processed so far.
int[] letter = new int[26];
int[] sumbol = new int[26];
int numberOfSymbolsProcessed = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lst = line.split(" ");
Now you have two arrays and an index into each, add the symbol and letter to the array as follows...
int[] letter = new int[26];
int[] sumbol = new int[26];
int numberOfSymbolsProcessed = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] lst = line.split(" ");
letter[numberOfSymbolsProcessed] = lst[0];
sumbol[numberOfSymbolsProcessed] = lst[1];
numberOfSymbolsProcessed = numberOfSymbolsProcessed + 1;
This would be an excellent usecase for the List interface.
List<String> list = new LinkedList<String>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
list.addAll(Arrays.asList(line.split(" ")));
}
If you know that your file will either have letters or symbols, then, what you can do is to use the Pattern class and use a regular expression such as
^[a-z][A-Z]+$
to check if the given string, in your case it will be lst[i] has one or more letters. The ^ at the beginning and $ at the end ensure that you have only letters in the string.
If the string matches the pattern, than you know that it is a letter, so you can add it to the Letter list. If it does not, you can add it to the symbol data structure.
I recommend that you do not use arrays, but rather dynamic data structures such as an ArrayList for your lists since this will grow dynamically as you add elements to it.
For more information regarding the pattern class, you can check this tutorial
Related
Hi i'm relatively new to java and i'm wondering how to go about parsing a certain file format into a 2D array.
The file format consists of a a comma separating values with < and /> separating an addition set of values.
<a,b,c/><x,y,z>
<...
<...
Each line will then be inputted into an array[ ] [ ] where the first set will go into the first column and the next set to the second.
The line should then be outputted to look like this.
a, b, c
x ,y ,z
...
Any help would be great thanks.
EDIT: this is what i have so far
public static main (String args[])
{
//Open file, read to get number of lines of file = numLine
int[][] array = new int[numLine][numLine]
for (int i = 0; i < numLine; i++)
{
//Unsure how to write element/line split
array[i][i] = //input each element to array
}
}
}
You can modify this to suit your need. I added some comments and so you might want to pay attention to them.
Scanner sc = new Scanner(file);
String[][] array = new String[numLine][numLine];//declaring the matrix
int r=0 , c=0;//declaring the index of the matrices column and row
while (sc.hasNextLine()) {
String line = sc.nextLine();
line = line.replaceAll("[<>]", "");//removing > and < so we gonna have a,b,c/x,y,z
String[] col = line.split("/");// spliting using / and we gonna have a,b,c x,y,z
for (String row : col) {
//a,b,c or x,y,z
String[] oneCol = row.split(",");
for (String oneRow : oneCol) {
if(c >= numLine){
c = 0;
break;
}
array[r][c] = oneRow;
c++;
}
r++;
//System.out.println();
}
c = 0;
}
sc.close();
As #Young Millie pointed out, what have you attempted so far? That being said, there are several approaches you may take, one of them being the following.
A valid attempt would be to read the file line by line, then remove all occurrences of the symbols using replaceAll(...) (which is further explained in their java docs) but instead you could use the following replace:
String line = "<a,b,c/><x,y,z>";
line = line.replaceAll("[<>]", "");
System.out.println("1. " + line);
with a result of:
1. a,b,c/x,y,z
and then we split split the string on "/", resulting in two arrays of your required strings:
String[] lines = line.split("/");
System.out.println("1. " + lines[0]);
System.out.println("2. " + lines[1]);
with a result of:
1. a,b,c
2. x,y,z
I know this question has been already asked several times but I can't find the way to apply it on my code.
So my propose is the following:
I have two files griechenland_test.txt and outagain5.txt . I want to read them and then get which percentage of outagain5.txt is inside the other file.
Outagain5 has input like that:
mit dem 542824
und die 517126
And Griechenland is an normal article from Wikipedia about that topic (so like normal text, without freqeuncy Counts).
1. Problem
- How can I split the input in bigramms? Like every two words, but always with the one before? So if I have words A, B, C, D --> get AB, BC, CD ?
I have this:
while ((sCurrentLine = in.readLine()) != null) {
// System.out.println(sCurrentLine);
arr = sCurrentLine.split(" ");
for (int i = 0; i < arr.length; i++) {
if (null == hash.get(arr[i])) {
hash.put(arr[i], 1);
} else {
int x = hash.get(arr[i]) + 1;
hash.put(arr[i], x);
}
}
Then I read the other file with this code ( I just add the word, and not the number (I split it with 4 spaces, so the two words are at h[0])).
for (String line = br.readLine(); line != null; line = br.readLine()) {
String h[] = line.split(" ");
words.add(h[0]);
}
2. Problem
Now I make the comparsion between the String x in hash and the String s in words. I have put the else System out.print to get which words are not contained in outagain5.txt, but there are several words printed out which ARE contained in outagain5.txt. I don't understand why :D
So I think that the comparsion doesn't work well or maybe this will be solved will fix the first problem.
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> neuS = new ArrayList<String>();
ArrayList<Long> neuZ = new ArrayList<Long>();
for (String x : hash.keySet()) {
summe = summe + hash.get(x);
long neu = hash.get(x);
for (String s : words) {
if (x.equals(s)) {
neuS.add(x);
neuZ.add(neu);
disc = disc + 1;
} else {
System.out.println(x);
break;
}
}
}
Hope I made my question clear, thanks a lot!!
public static List<String> ngrams(int n, String str) {
List<String> ngrams = new ArrayList<String>();
String[] words = str.split(" ");
for (int i = 0; i < words.length - n + 1; i++)
ngrams.add(concat(words, i, i+n));
return ngrams;
}
public static String concat(String[] words, int start, int end) {
StringBuilder sb = new StringBuilder();
for (int i = start; i < end; i++)
sb.append((i > start ? " " : "") + words[i]);
return sb.toString();
}
It is much easier to use the generic "n-gram" approach so you can split every 2 or 3 words if you want. Here is the link I used to grab the code from: I have used this exact code almost any time I need to split words in the (AB), (BC), (CD) format. NGram Sequence.
If I recall, String has a method titled split(regex, count) that will split the item according to a specific point and you can tell it how many times to do it.
I am referencing this JavaDoc https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String, int).
And I guess for running comparison between two text files I would recommend having your code read both of them, populated two unique arrays and then try to run comparisons between the two strings each time. Hope I helped.
I want to delete all the vowels from every word inside of an array. So I came up with this:
public class AnotherExercise {
public static void main (String[] args){
String[] intruments = {"cello", "guitar", "violin", "double bass"}; //the array of words
String[] vowels = {"a", "e", "i", "o", "u"}; // the array of vowels
String[] intruments2 = new String[5]; //the array of wrods without vowels
String nothing = ""; // used to replace a vowel to nothing
for (int i = 0; i < 4; i++){ // first for loop to go through all of the intruments
for(int e = 0; e < 5; e++){ //second for loop to go through all the vowels
intruments2[i] = intruments[i].replaceAll(vowels[e], nothing); //replacing vowels
//and storing them in a new array
}
System.out.println(intruments2[i]); //outputting the words without vowels
}
}
}
From all the options I tried, I guess this one was the best, but still I can't make it work, it outputs:
cello
gitar
violin
doble bass
The weirdest part in my opinion is that it does replace "u". It may be a silly mistake but I can't figure it out.
Method String.ReplaceAll receives regex as first argument. It allows you to remove all vowels in one shot:
public class AnotherExercise {
public static void main (String[] args){
String[] intruments = {"cello", "guitar", "violin", "double bass"};
String[] intruments2 = new String[5];
String nothing = "";
for (int i = 0; i < 4; i++){
intruments2[i] = intruments[i].replaceAll("a|e|i|o|u", nothing);
System.out.println(intruments2[i]);
}
}
}
And, yes, about the problem with your version. In the inner loop you always apply replacement to initial version of the string. So you get results only from the last attempt ‒ letter 'u'.
int x = 0;
String[] QEquivalent = {};
String s = sc.nextLine();
String[] question2 = s.split(" ");
for (int i = 0; i < question2.length; i++) {
System.out.println(question2[i]);
x++;
} //debug
System.out.println(x);
String s2 = sc2.nextLine();
String[] Answer = s2.split(" ");
for (int c = 0; c < Answer.length; c++) {
System.out.println(Answer[c]);
} //debug
int y;
String u = sn.nextLine();
String[] t = u.split(" ");
for (y = 0; y < question2.length; y++) {
for (int w = 0; w < t.length; w++) {
if (t[w].equals(question2[y])) {
QEquivalent[y] = "ADJ";
System.out.println(QEquivalent[y]);
break;
}
}
}
this is the line of codes that I have as of now. when a string in question2 is found in String[] t, it should store the string "ADJ" in String[] QEquivalent. I can't seem to fix the error. can someone please help me?
You are creating an empty array here:
String[] QEquivalent = {};
So, any index you try to access will be out of bounds. You should creating an array using a fixed size.
Or, you can better use an ArrayList instead, which can dynamically grow in size:
List<String> qEquivalent = new ArrayList<String>();
and then add elements using:
qEquivalent.add("ADJ");
And please follow Java Naming conventions. Variable names should start with lowercase letters.
You create an empty array:
String[] QEquivalent = {};
and then set some elements at index y > 0:
QEquivalent[y] = "ADJ";
You can either:
compute the final dimension of the array and be sure to instantiate it: String[] QEquivalent = new String[SIZE];
use a dynamic structure like an ArrayList
eg:
ArrayList<String> QEquivalent = new ArrayList<QEquivalent>();
QEquivalent.add("ADJ");
Your array QEquivalent is an empty array . It is of length 0 , hence even QEquivalent[0] will throw ArrayIndexOutOfBoundsException.
One fix I can see is assign it a length :
String[] question2 = s.split(" ");
// Just assign the dimension till which you will iterate finally
// from your code `y < question2.length` it seems it should be question2.length
// Note you are always indexing the array using the outer loop counter y
// So even if there are n number of nested loops , assigning the question2.length
// as dimension will work fine , unless there is something subtle you missed
// in your code
String[] QEquivalent = new String[question2.length];
Better use any implementation of List , like an ArrayList.
List<String> qEquivalent = new ArrayList<String>();
......
if (t[w].equals(question2[y])) {
qEquivalent.add("ADJ");
System.out.println(qEquivalent.get(y));
break;
}
Give some size to the array String[] QEquivalent = new String[100];
You statement String[] QEquivalent = {}; creates an array with zero size.
You are declaring your QEquivalent array as an empty String array.
When you access the index QEquivalent[y], that index doesn't exist, hence the ArrayIndexOutOfBoundsException.
I strongly suggest you use a List<String> instead.
Such as:
List<String> qEquivalent = new ArrayList<String>(); // replaces the array declaration and uses Java conventional naming
...
qEquivalent.add("ADJ"); // replaces the indexing of the array and adds the item
Possibly QEquivalent variable makes the error.Because when you declare that variable, its length is 0.So declare the variable as with new and a size.
Or move it after you split the string into question2 and use:
String[] QEquivalent = new String[question2.length];
I have a simple java statement along the lines of:
String[] ykl = yklList.get(0).split(" ");
where, yklList is an ArrayList containing sentences which are split into words. The above works fine.
When I now try and loop this:
Loop:1
for (int i=0;i<=4;i++)
{
String[] ykl = yklList.get(i).split(" ");
}
It does not seem to work and throws me a compile error. Is the above loop wrong?
I have another for loop after the above:
Loop:2
for (String ykl : yklList)
{
//do something
}
It throws me a compile error here saying:
cannot find symbol
symbol : variable ykl
location: class test
for (String ykl : yklList)
Presumably Loop 1 went wrong somewhere?
Edit:
The "full" code looks something like this:
for (int i=0;i<=yklList.size()-1;i++)
{
String[] ykl = yklList.get(i).split(" ");
}
for (String y : ykl)
{
t.add(y);
}
and the error is:
cannot find symbol
symbol : variable ykl
location: class test
for (String y : ykl)
Regarding to your update:
You define ykl in the first loop. So the variable is not in the scope of the second loop. Your code have to look like this:
for (String yklSentence : yklList) {
String[] ykl = yklSentence.split(" ");
for (String y : ykl) {
t.add(y);
}
}
By the way: If you would use an IDE like Eclipse you would see those errors in the editor!
In your appendix, the second cycle does not know about the variable ykl, as defined in the previous cycle and after leaving it - it is destroyed.
If variables were not removed - after a while all of the available computer memory would be filled, which would cause an error of the program. If only you were given the opportunity dal unnecessary variables - that is allowed a situation where you forgot to remove unneeded variables and memory, again, would be full. In this context, Java simplifies the development and can not think about what you are then permennye not removed.
To correct the error:
ArrayList <String> yklList = new ArrayList <String> ();
ArrayList <String> t = new ArrayList <String> ();
for (int i = 0; i <yklList.size (); i + +)
{
String [] ykl = yklList.get (i). Split ("");
for (String y: ykl)
t.add (y);
}
or:
If you fill out absolutely all characters t of yklList:
ArrayList<String> yklList = new ArrayList<String>();
ArrayList<String> t = new ArrayList<String>();
String[] ykl = new String[0];
for (int i=0;i<yklList.size();i++) {
String[] tempYkl = yklList.get(i).split(" ");
String[] tempYklDub = new String[tempYkl.length];
String[] tempYklDubDub = new String[ykl.length];
for (int z=0; z< tempYkl.length; z++)
tempYklDub[z] = tempYkl[z];
for (int z=0; z<ykl.length; z++)
tempYklDubDub[z]=ykl[z];
ykl = new String[tempYklDubDub.length+tempYklDub.length];
for (int z=0; z<tempYklDubDub.length; z++)
ykl[z]=tempYklDubDub[z];
for (int z=tempYklDubDub.length, k=0; k<tempYklDub.length; z++, k++)
ykl[z]=tempYklDub[k];
}
for (String y : ykl)
t.add(y);
If I understood you correctly, you have less than 4 yklList elements.
And, at the next iteration, it tries to take an element from outside the yklList
Solutions to the problem:
for (int i = 0; i <yklList.size(); i + +) {
String[] ykl = yklList.get(i).split(" ");
}
or
for (String yklListDub: yklList) {
String[] ykl = yklListDub.split(" ");
}
P.S. I'm sorry for my english)
You should make sure looping the right count of elements.
Try using size() for your ArrayList:
for (int i=0;i<ykl.size();i++)
{
String[] ykl = yklList.get(i).split(" ");
}
Keep in mind that every cycle overwrites your ykl array. Defining it outside of your loop would solve this problem.
Edit:
Try
String[] ykl = new String[ykl.size()];
for (int i=0;i<ykl.size();i++)
{
ykl[i] = yklList.get(i).split(" ");
}
for (String y : ykl)
{
t.add(y);
}