So I have an array that can store at least values. I let my user enter 3 times using for loop. The question is here is that, when the user enter the same key the value will overwrite. How to put the value into next index?
here is my entire code so far:
import java.util.*;
import java.io.*;
public class low{
public static void main(String[] args)throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int key = 0;
// StringBuffer value = new StringBuffer();
String value = "";
ArrayList<String> hash = new ArrayList<String>();
String[] arr = new String[5];
for(int i = 0; i < 3; i++){
System.out.println("Key: ");
key = Integer.parseInt(sc.readLine());
System.out.println("Value: ");
value = sc.readLine();
key++;
arr[key] = value;
}
for(int x = 0; x < arr.length; x++){
System.out.println("Element at index " + x + " : "+ arr[x]);
}
}
}
You may need a Map to store the values rather than array
Scanner in = new Scanner(System.in);
Map<Integer,String> map = new HashMap<>();
for(int i=0; i<3; i++)
{
int key = Integer.parseInt(in.nextLine().trim());
if(key < 0 || key > 4) // it looks like you want the keys stay between [0,5)
throw new IllegalArgumentException("invalid key");
String value = in.nextLine();
map.put(key,value);
}
for(Map.Entry<Integer,String> entry : map.entrySet())
{
System.out.printf("Element at index %d : %s\n",entry.getKey(),entry.getValue());
}
Instead of
key++;
arr[key] = value;
Do
if(arr[key] == null)
arr[key + 1] = value;
else
arr[key] = value;
But you’ll overwrite the information at index key + 1. You might want to rethink how you’re adding data.
Related
I want to read an file, and want to collect top n words depends on word frequency.
I have tried the following code to count every words in a string.
public static void main(String[] args) throws FileNotFoundException, IOException {
FileReader fr = new FileReader("txtFile.txt");
BufferedReader br = new BufferedReader(fr);
String text = "";
String sz = null;
while ((sz = br.readLine()) != null) {
text = text.concat(sz);
}
String[] words = text.split(" ");
String[] uniqueLabels;
int count = 0;
System.out.println(text);
uniqueLabels = getLabels(words);
for (String l: uniqueLabels) {
if ("".equals(l) || null == l) {
break;
}
for (String s: words) {
if (l.equals(s)) {
count++;
}
}
System.out.println("Word :: " + l + " Count :: " + count);
count = 0;
}
}
And I used the following code to collect unique lbels(words) get if from link,
private static String[] getLabels(String[] keys) {
String[] uniqueKeys = new String[keys.length];
uniqueKeys[0] = keys[0];
int uniqueKeyIndex = 1;
boolean keyAlreadyExists = false;
for (int i = 1; i < keys.length; i++) {
for (int j = 0; j <= uniqueKeyIndex; j++) {
if (keys[i].equals(uniqueKeys[j])) {
keyAlreadyExists = true;
}
}
if (!keyAlreadyExists) {
uniqueKeys[uniqueKeyIndex] = keys[i];
uniqueKeyIndex++;
}
keyAlreadyExists = false;
}
return uniqueKeys;
}
And this works fine, I want to collect top 10 ranked words depend on it's frequency in file.
First of all, if you want it to run moderately fast, don't loop trough all the Strings in an array...use a HashMap... or even find some map for primitives.
Then go through the words. If the words is in the map, increment the value, otherwise put a 1.
In the end, sort the map entries and fetch the first 10.
Not a total duplicate, but this answer pretty much shows how to get the counting done: Calculating frequency of each word in a sentence in java
I recommend using a Hashmap<String, Integer>() to count the word frequency. Hash uses key-value-pairs. That means the key is unique (your word) and the value variable. If you perform a put operation with a already existing key, the value will be updated.
Hashmap
Something like this should work:
hashmap.put(key, hashmap.get(key) + 1);
To get the top then words, I would implement sort the hashmap and retrieve the first ten entries.
I solved it as,
public class wordFreq {
private static String[] w = null;
private static int[] r = null;
public static void main(String[] args){
try {
System.out.println("Enter 'n' value :: ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
w = new String[n];
r = new int[n];
FileReader fr = new FileReader("acq.txt");
BufferedReader br = new BufferedReader(fr);
String text = "";
String sz = null;
while((sz=br.readLine())!=null){
text = text.concat(sz);
}
String[] words = text.split(" ");
String[] uniqueLabels;
int count = 0;
uniqueLabels = getUniqLabels(words);
for(int j=0; j<n; j++){
r[j] = 0;
}
for(String l: uniqueLabels)
{
if("".equals(l) || null == l)
{
break;
}
for(String s : words)
{
if(l.equals(s))
{
count++;
}
}
for(int i=0; i<n; i++){
if(count>r[i]){
r[i] = count;
w[i] = l;
break;
}
}
count=0;
}
display(n);
} catch (Exception e) {
System.err.println("ERR "+e.getMessage());
}
}
public static void display(int n){
for(int k=0; k<n; k++){
System.out.println("Label :: "+w[k]+"\tCount :: "+r[k]);
}
}
private static String[] getUniqLabels(String[] keys)
{
String[] uniqueKeys = new String[keys.length];
uniqueKeys[0] = keys[0];
int uniqueKeyIndex = 1;
boolean keyAlreadyExists = false;
for(int i=1; i<keys.length ; i++)
{
for(int j=0; j<=uniqueKeyIndex; j++)
{
if(keys[i].equals(uniqueKeys[j]))
{
keyAlreadyExists = true;
}
}
if(!keyAlreadyExists)
{
uniqueKeys[uniqueKeyIndex] = keys[i];
uniqueKeyIndex++;
}
keyAlreadyExists = false;
}
return uniqueKeys;
}
}
And the sample output is,
Enter 'n' value ::
5
Label :: computer Count :: 30
Label :: company Count :: 22
Label :: express Count :: 20
Label :: offer Count :: 16
Label :: shearson Count :: 16
For instance suppose I have the following String
String S = "5,a\n" +
"6,b\n" +
"9,a";
The format is always the same - one digit, then comma, then one character and then line end character.
For looping each row in String I use
for(String a : S.split("\\n")){}
I want to learn the character with highest amount, when grouped by character. For Instance, there is only one "b", so value is 6; whereas "a" has two lines, so its value is 5 + 9 = 14. Since 14 is maximum here, I want to find out "a" and 14 and save them in variables.
You can do something like below :
public static void main (String[] args) throws java.lang.Exception
{
String S = "5,a\n" +
"6,b\n" +
"9,a";
String[] lines = S.split("\\n");
Map<String, Integer> map = new HashMap<String, Integer>();
for( String t : lines )
{
String[] e = t.split(",");
Integer digit = Integer.parseInt(e[0]);
String c = e[1];
if ( map.get(c) != null )
{
Integer val = map.get(c);
val += digit;
map.put( c, val );
}
else
{
map.put( c, digit );
}
}
int max = 0;
String maxKey = null;
for ( String k : map.keySet() )
{
if ( map.get(k) > max )
{
max = map.get(k);
maxKey = k;
}
}
System.out.println("The maximum key is : " + maxKey );
System.out.println("The maximum value is : " + max );
}
Output is :
The maximum key is : a
The maximum value is : 14
Use a HashMap to store each pair, with the letter as the key. If the entry doesn't exist, put the first number. If it exists, get the entry and add the number, and then put the sum.
import java.util.HashMap;
import java.util.Map;
public class ParseTest {
public static void main(String[] args) {
String S = "5,a\n" + "6,b\n" + "9,a";
String maxKey = null;
int maxVal = 0;
Map<String, Integer> sums = new HashMap<>();
for (String a : S.split("\\n")) {
String[] split = a.split(",");
int value = Integer.parseInt(split[0]);
String key = split[1];
if (sums.containsKey(key)) {
sums.put(key, sums.get(key) + value);
} else {
sums.put(key, value);
}
if (sums.get(key) > maxVal) {
maxVal = sums.get(key);
maxKey = key;
}
}
System.out.println("Max key: " + maxKey + ", Sum: " + maxVal);
}
}
After finishing my answer, I found that many similar answers have been posted out :). Anyway, my solution:
public static void main(String[] args) {
String S = "5,a\n6,b\n9,a";
Map<String, Integer> map = new HashMap<String, Integer>();
String highestAmountChar = "";
int highestAmount = 0;
for (String str : S.split("\\n")) {
String[] amountChar = str.split(",");
if (map.get(amountChar[1]) == null) {
map.put(amountChar[1], Integer.parseInt(amountChar[0]));
} else {
map.put(amountChar[1], map.get(amountChar[1]) + Integer.parseInt(amountChar[0]));
}
if (highestAmount < map.get(amountChar[1])) {
highestAmount = map.get(amountChar[1]);
highestAmountChar = amountChar[1];
}
}
System.out.println("The character " + highestAmountChar + " has highest amount " + highestAmount);
}
You could use something like this without using HashMap or any collection for that matter
import java.util.Arrays;
public class Test {
public static void main(String args[]) {
String S = "5,a\n" +
"6,b\n" +
"9,a";
// Separate the string by number and letter
String[] separated = S.split("\\n");
// Create a new array to store the letters only
char[] letters = new char[separated.length];
// Write the letter
for (int i = 0; i < letters.length; i++) {
letters[i] = separated[i].charAt(2);
}
// Sort them haha
Arrays.sort(letters);
// And now find out which letter is repeated most
// Store the first letter
char previous = letters[0];
// Make it the most repeated one for now
char mostRepeated = letters[0];
int count = 1;
int maxCount = 1;
for (int i = 1; i < letters.length; i++) {
// since the array is sorted if the actual letter is the same as the previous one then keep counting
if (letters[i] == previous)
count++;
else {
if (count > maxCount) {
mostRepeated = letters[i - 1];
maxCount = count;
}
previous = letters[i];
count = 1;
}
}
char answer = count > maxCount ? letters[letters.length-1] : mostRepeated;
// Once you get the letter now just add all the numbers that goes with it
int sum = 0;
for (String s:separated) {
if (s.charAt(2) == answer) {
sum += Character.getNumericValue(s.charAt(0));
}
}
// Print the result by printing the letter and it sum
}
}
After hard searchig I still haven't found the proper answer for my question and there is it:
I have to write a java program that enters an array of strings and finds in it the largest sequence of equal elements. If several sequences have the same longest length, the program should print the leftmost of them. The input strings are given as a single line, separated by a space.
For example:
if the input is: "hi yes yes yes bye",
the output should be: "yes yes yes".
And there is my source code:
public static void main(String[] args) {
System.out.println("Please enter a sequence of strings separated by spaces:");
Scanner inputStringScanner = new Scanner(System.in);
String[] strings = inputStringScanner.nextLine().split(" ");
System.out.println(String.join(" ", strings));
ArrayList<ArrayList<String>> stringsSequencesCollection = new ArrayList<ArrayList<String>>();
ArrayList<String> stringsSequences = new ArrayList<String>();
stringsSequences.add(strings[0]);
for (int i = 1; i < strings.length; i++) {
if(strings[i].equals(strings[i - 1])) {
stringsSequences.add(strings[i]);
} else {
System.out.println(stringsSequences + " " + stringsSequences.size());
stringsSequencesCollection.add(stringsSequences);
stringsSequences.clear();
stringsSequences.add(strings[i]);
//ystem.out.println("\n" + stringsSequences);
}
if(i == strings.length - 1) {
stringsSequencesCollection.add(stringsSequences);
stringsSequences.clear();
System.out.println(stringsSequences + " " + stringsSequences.size());
}
}
System.out.println(stringsSequencesCollection.size());
System.out.println(stringsSequencesCollection.get(2).size());
System.out.println();
int maximalStringSequence = Integer.MIN_VALUE;
int index = 0;
ArrayList<String> currentStringSequence = new ArrayList<String>();
for (int i = 0; i < stringsSequencesCollection.size(); i++) {
currentStringSequence = stringsSequencesCollection.get(i);
System.out.println(stringsSequencesCollection.get(i).size());
if (stringsSequencesCollection.get(i).size() > maximalStringSequence) {
maximalStringSequence = stringsSequencesCollection.get(i).size();
index = i;
//System.out.println("\n" + index);
}
}
System.out.println(String.join(" ", stringsSequencesCollection.get(index)));
I think it should be work correct but there is a problem - the sub array list's count isn't correct: All the sub arrayList's size is 1 and for this reason the output is not correct. I don't understand what is the reason for this. If anybody can help me to fix the code I will be gratefull!
I think it is fairly straight forward just keep track of a max sequence length as you go through the array building sequences.
String input = "hi yes yes yes bye";
String sa[] = input.split(" ");
int maxseqlen = 1;
String last_sample = sa[0];
String longest_seq = last_sample;
int seqlen = 1;
String seq = last_sample;
for (int i = 1; i < sa.length; i++) {
String sample = sa[i];
if (sample.equals(last_sample)) {
seqlen++;
seq += " " + sample;
if (seqlen > maxseqlen) {
longest_seq = seq;
maxseqlen = seqlen;
}
} else {
seqlen = 1;
seq = sample;
}
last_sample = sample;
}
System.out.println("longest_seq = " + longest_seq);
Lots of issues.
First of all, when dealing with the last string of the list you are not actually printing it before clearing it. Should be:
if(i == strings.length - 1)
//...
System.out.println(stringsSequences + " " + stringsSequences.size());
stringsSequences.clear();
This is the error in the output.
Secondly, and most importantly, when you do stringsSequencesCollection.add you are adding an OBJECT, i.e. a reference to the collection. When after you do stringsSequences.clear(), you empty the collection you just added too (this is because it's not making a copy, but keeping a reference!). You can verify this by printing stringsSequencesCollection after the first loop finishes: it will contain 3 empty lists.
So how do we do this? First of all, we need a more appropriate data structure. We are going to use a Map that, for each string, contains the length of its longest sequence. Since we want to manage ties too, we'll also have another map that for each string stores the leftmost ending position of the longest sequence:
Map<String, Integer> lengths= new HashMap<>();
Map<String, Integer> indexes= new HashMap<>();
String[] split = input.split(" ");
lengths.put(split[0], 1);
indexes.put(split[0], 0);
int currentLength = 1;
int maxLength = 1;
for (int i = 1; i<split.length; i++) {
String s = split[i];
if (s.equals(split[i-1])) {
currentLength++;
}
else {
currentLength = 1;
}
int oldLength = lengths.getOrDefault(s, 0);
if (currentLength > oldLength) {
lengths.put(s, currentLength);
indexes.put(s, i);
}
maxLength = Math.max(maxLength, currentLength);
}
//At this point, youll have in lengths a map from string -> maxSeqLengt, and in indexes a map from string -> indexes for the leftmost ending index of the longest sequence. Now we need to reason on those!
Now we can just scan for the strings with the longest sequences:
//Find all strings with equal maximal length sequences
Set<String> longestStrings = new HashSet<>();
for (Map.Entry<String, Integer> e: lengths.entrySet()) {
if (e.value == maxLength) {
longestStrings.add(e.key);
}
}
//Of those, search the one with minimal index
int minIndex = input.length();
String bestString = null;
for (String s: longestStrings) {
int index = indexes.get(s);
if (index < minIndex) {
bestString = s;
}
}
System.out.println(bestString);
Below code results in output as you expected:
public static void main(String[] args) {
System.out.println("Please enter a sequence of strings separated by spaces:");
Scanner inputStringScanner = new Scanner(System.in);
String[] strings = inputStringScanner.nextLine().split(" ");
System.out.println(String.join(" ", strings));
List <ArrayList<String>> stringsSequencesCollection = new ArrayList<ArrayList<String>>();
List <String> stringsSequences = new ArrayList<String>();
//stringsSequences.add(strings[0]);
boolean flag = false;
for (int i = 1; i < strings.length; i++) {
if(strings[i].equals(strings[i - 1])) {
if(flag == false){
stringsSequences.add(strings[i]);
flag= true;
}
stringsSequences.add(strings[i]);
}
}
int maximalStringSequence = Integer.MIN_VALUE;
int index = 0;
List <String> currentStringSequence = new ArrayList<String>();
for (int i = 0; i < stringsSequencesCollection.size(); i++) {
currentStringSequence = stringsSequencesCollection.get(i);
System.out.println(stringsSequencesCollection.get(i).size());
if (stringsSequencesCollection.get(i).size() > maximalStringSequence) {
maximalStringSequence = stringsSequencesCollection.get(i).size();
index = i;
//System.out.println("\n" + index);
}
}
System.out.println(stringsSequences.toString());
I have a text file of 50 string lines of varying length and content. I need to read the file and sort in ascending order. Sorting condition: the number of words in a sentence that start from the letter "a".
public static void main(String[] args) throws FileNotFoundException {
String token1 = "";
Scanner inFile1 = new Scanner(new File("E:\\text.txt"));
List<String> temps = new LinkedList<String>();
inFile1.useDelimiter(". ");
while (inFile1.hasNext()) {
token1 = inFile1.nextLine();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);
for (int i = 0; i < tempsArray.length; i++) {
System.out.println(tempsArray[i]);
}
int cnt = 0; //number of words in the string line
for (int i=0; i<tempsArray.length; i++) {
int k=0; //number of words that start from the letter "а"
System.out.println("Line № = " + i);
StringTokenizer st = new StringTokenizer(tempsArray[i]);
while (st.hasMoreTokens()) {
cnt++;
String s= st.nextToken();
if (s.charAt(0)=='a') {
k++;
}
}
System.out.println("Number of words = " + cnt);
cnt=0;
System.out.println("Number of words 'а' = " + k);
}
}
I use Map as Kau advise me. But Map use unique keys. But my K can have same values and Map can't find an appropriate string element. What other Сollection сan I use?
I am assuming you already have the algorithm for shell short to sort an array of integers. Let the method be shellSort(int[] a).
What you can do is create a map with key as k and value as the string representing the line. At the same time we'll create an array of integers that holds all k . Then call the method shellSort on the array of k values. Then read back from the sorted array, look in the map using the array elements as keys. Fetch the corresponding map values (which are the lines) and put them back one by one in tempsArray which should finally have all the lines sorted in the desired way.
Below is the code (untested) just to give an idea.
public static void main(String[] args) throws FileNotFoundException {
String token1 = "";
Scanner inFile1 = new Scanner(new File("E:\\text.txt"));
List<String> temps = new LinkedList<String>();
inFile1.useDelimiter(". ");
while (inFile1.hasNext()) {
token1 = inFile1.nextLine();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);
for (int i = 0; i < tempsArray.length; i++) {
System.out.println(tempsArray[i]);
}
int cnt = 0; //number of words in the string line
Map<Integer, List<String>> myMap = new HashMap<Integer, List<String>>();
int[] countArr = new int[tempsArray.length];
for (int i=0; i<tempsArray.length; i++) {
int k=0; //number of words that start from the letter "а"
System.out.println("Line № = " + i);
StringTokenizer st = new StringTokenizer(tempsArray[i]);
while (st.hasMoreTokens()) {
cnt++;
String s= st.nextToken();
if (s.charAt(0)=='a') {
k++;
}
}
countArr[i] = k;
List<String> listOfLines = myMap.get(k);
if(listOfLines == null){
listOfLines = new ArrayList<String>();
listOfLines.add(tempsArray[i]);
myMap.put(k, listOfLines);
} else{
listOfLines.add(tempsArray[i]);
}
System.out.println("Number of words = " + cnt);
cnt=0;
System.out.println("Number of words 'а' = " + k);
}
//Call shellsort here on the array of k values
shellSort(countArr);
List<String> sortedListOfLines = new ArrayList<String>();
for(int i=0; i<countArr.length; i++){
List<String> lineList = myMap.get(countArr[i]);
if(lineList != null){
sortedListOfLines.addAll(lineList);
lineList = null;
myMap.put(countArr[i], lineList);
}
}
}
I am writing a program to take in a tweet as input and return a value of the unique hashtags used in the tweet. However in the countUniqueHashtags method, my hashtagCount variable will only return a value of 1 if the input contains hashtags(even if there is more than one) and a value of 0 if the input doesn't contain any hashtags.
public class UniqueHashtagCounter
{
static ArrayList<String> uniqueHashTags = new ArrayList<String>();
int numberOfTweetsToFollow;
public String tweetSpace = "";
Scanner in = new Scanner(System.in);
public int getTweetsToFollow()
{
System.out.println("Enter the number of Tweets you wish to follow: ");
numberOfTweetsToFollow = in.nextInt();
return numberOfTweetsToFollow;
}
public String tweetsInput()
{
for(int i = 0; i <= numberOfTweetsToFollow ; ++i)
{
if(in.hasNextLine()){
tweetSpace = tweetSpace + in.nextLine();
}
}
return tweetSpace;
}
public ArrayList<String> populateArray()
{
uniqueHashTags.add(tweetSpace);
for(String s: uniqueHashTags)
s.toLowerCase().split(" ");
for(int x = 0; x < uniqueHashTags.size(); x++){
countUniqueHashtags(uniqueHashTags);}
return uniqueHashTags;
}
static void countUniqueHashtags(ArrayList<String> strings)
{
int hashtagCount = uniqueHashTags.size();
ListIterator<String> listIterator = strings.listIterator();
while(listIterator.hasNext())
{
String e = listIterator.next();
if(e.startsWith("#"))
hashtagCount = hashtagCount + 1;
}
System.out.println("The number of unique hashtags is: " + hashtagCount);
}
"My hashtagCount variable will only return a value of 1 if the input contains hashtags(even if there is more than one) and a value of 0 if the input doesn't contain any hashtags"
That's because you are using startsWith():
if(e.startsWith("#"))
hashtagCount = hashtagCount + 1;
You need to loop through the string and count the hashtag characters:
for(int i=0; e.length();i++){
if(e.charAt(i)=='#') hashtagcount++;
}