java


2D array prints unstable output


My test program here is supposed to take a 1D array and sort it into a 2D array.
The file is 3000 common words from 'a' to 'z'. The setup seems correct and I even get an output that starts out correct. However, after words starting with 'b' I get nothing but nulls.
public static void main(String[] args) throws FileNotFoundException {
int listSize = 0;
File file = new File("3000-Common-Words.txt");
Initialize a = new Initialize();
String[] oneArray = a.Initialization(file);
for (int i = 0; i < 26; i++) {
int sizeCheck = 0;
for (int j = 0; j < 3000; j++) {
if (oneArray[j].charAt(0) == (char) (i + 97)) {
sizeCheck++;
}
if (sizeCheck > listSize) {
listSize = sizeCheck;
}
}
}
String[][] twoArray = new String[26][listSize];
for (int i = 0; i < 26; i++) {
for (int j = 0; j < listSize; j++) {
if (oneArray[j].charAt(0)==(char)(i+97)){
twoArray[i][j]=oneArray[j];
}
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < listSize; j++) {
System.out.println(twoArray[i][j]);
}
}
}
The initialisation of the first array is correct (I've checked the output).
I see why my array stops before 'c'. Its because I've got oneArray[j] but I'm still stuck on how to fill the array...
Any help is appreciated!
The logic to populate the array is slightly flawed.
You have correctly worked out the minimum dimension for the sparse matrix.
It will be 26 by highest number of words in any letter of the alphabet.
However, you have not chosen independent iteration counters for reading the word list and populating the array.
Here is a working solution:
public class WordSort {
public static void main(String[] args) {
int listSize = 0;
String[] oneArray = {"a1","b1","c1","d1","e1","f1","g1","h1","i1","j1","k1","l1","m1","n1","o1","p1","q1","r1","s1","t1","u1","v1","w1","x1","y1","z1","a2","b2","d2","g2","n2","l2","z2","v2","g3","h3","p0","h9","r5","t3","o9","l7","h6","f5","q5",};
for (int i = 0; i < 26; i++) {
int sizeCheck = 0;
for (int j = 0; j < oneArray.length; j++) {
if (oneArray[j].charAt(0) == (char) (i + 97)) {
sizeCheck++;
}
if (sizeCheck > listSize) {
listSize = sizeCheck;
}
}
}
String[][] twoArray = new String[26][listSize];
for (int i = 0; i < 26; i++) {
int k = 0;
for (int j = 0; j < oneArray.length; j++) {
if (oneArray[j].charAt(0)==(char)(i+97)){
twoArray[i][k++]=oneArray[j];
}
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < listSize; j++) {
System.out.println(twoArray[i][j]);
}
}
}
}
sizeCheck will be the number of words that start with the letter.
listSize will be the maximum of those.
However in your second loop
for (int j = 0; j < listSize; j++) {
if (oneArray[j].charAt(0)==(char)(i+97)){
twoArray[i][j]=oneArray[j];
}
}
you always only "scan" the first "listSize" amount of words in the first array, which actually is 3000 words long. So don't use j for iterating thru "oneArray", instead use another variable for this.

Related Links

Trying to do a short circuit && as well as assigning error to a String?
Print some HashMap element
Wait for multiple elements to become invisible Selenium Java
Fatal Error JRE Android Studio
Java Android bulk Transfer between Android and rfid
Is there an online LIBXML2 XML parser available or a way to parse a xml with libxml2 standalone?
How to update files in AWS S3 maintaining high availability [closed]
SendKeys doesn't work in Selenium PhantomJS
Shrinking the JPanel in Swing
I just wanted to concatenate two strings in java
Android unable to connect my web-service in app
Error in text file returned with urlconnection
Tango app suddenly breaks with JNI DETECTED ERROR IN APPLICATION
command line argument and default value (how to reset)
Why is String concatenation faster than String.valueOf for converting an Integer to a String?
Array transformation

Categories

HOME
pdf
gremlin
office365api
iot
rsync
sqlite3
v8
jxls
pivotal-cloud-foundry
convolution
vault
volttron
floating-action-button
vb.net-2010
event-handling
iron-router
jsprit
serverless-framework
django-simple-history
worldwind
progressive-web-apps
tostring
jqwidget
google-cloud-speech
django-cms
nodatime
cloudhub
web-sql
tibco-mdm
smb
buildbot
microsoft-chart-controls
bluestacks
fabric8
media-queries
scorm2004
catch-all
framemaker
cloud-code
catalog
revolution-slider
plsql-psp
vapor
document.write
bootstrapper
greenrobot-eventbus
squib
gzipstream
total-commander
acoustics
skeleton-css-boilerplate
menuitem
chain-builder
react-scripts
errordocument
colorama
dtexec
python-cryptography
account-kit
google-cdn
setuptools
wdf
rotativa
dstu2-fhir
mikroc
clang-static-analyzer
pagedlist
packagist
yaws
rhino-servicebus
ptrace
historian
python-3.2
simple-framework
coveralls
ami
graph-api-explorer
facebook-graph-api-v2.4
offloading
gwidgets
network-interface
gui-test-framework
zend-route
has-many-through
tld
system.net.webexception
ember-charts
mbr
stxxl
prettify
commoncrypto
cdc
ftps
jquery-mobile-dialog
orchardcms-1.7
enterprisedb
robotics-studio
jspinclude
amazon-appstore
semantic-diff
locate
lpeg
yslow
wise
exchange-server-2003

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App