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

Java Hibernate delete cascade
Spring Authentication Required ERROR [duplicate]
Android Product Flavors for Java Web Projects
[Java][SQL] Hardcoded username and password on query
how to modify the value of a primitive data passed to a C++ routine using Java and JNI?
Java: test System output including “new lines” with assertEquals
How to ignore ' ' in Xpath?
Casting a type in a data structure
Spring Boot EnableCaching and Cacheable annotation not working
FATAL EXCEPTION: main NoSuchMethodError for VideoView.setOnPreparedListener() [duplicate]
Using SWIG to wrap c++ to java UnsatisfiedLinkError: HeaderJNI.new_Test()J
Java accessing object throught array
JavaFX ScrollPane setVvalue() not working as intended
Efficient handling of mouse clicks
Right syntax for concat on derby
Break 2 loops if condition satisfied in Java [duplicate]

Categories

HOME
compiler-construction
variables
testng
wso2
mediawiki
reserved
path-finding
plot
include
relayjs
bookshelf.js
at-command
amortized-analysis
electronics
packages
directx
nano-server
circuit
openrefine
jsdoc
openedx
firefox-webextensions
riot.js
hammerspoon
one-to-many
vlsi
dynamic-featured-image
react-css-modules
tasklet
file-rename
srcset
lightswitch-2013
titanium-mobile
scorm2004
protovis
subset-sum
normal-distribution
http-digest
revolution-slider
dartium
swift3.0.2
android-browser
abstract-class
xenforo
webix-treetable
leading-zero
galen
bootstrap-dialog
netcdf4
convertapi
errordocument
multipeer-connectivity
powercli
lowpass-filter
mplayer
forever
nsarray
zendesk-app
imanage
topbeat
time-and-attendance
wdf
dstu2-fhir
firebaseui
gridview-sorting
lttng
xna-4.0
lemon
jmeter-maven-plugin
varargs
android-listview
system32
p2
key-management
pretty-print
mdt
java-metro-framework
document-classification
google-reader
aapt
uitouch
balanced-payments
viadeo
gnu-smalltalk
pyhdf
mysqltuner
aqtime
dir
django-nonrel
yui-compressor
type-equivalence
angularjs-controller
anonymous-methods
typoscript2
hamiltonian-cycle
coderush
bluepill
venn-diagram
dbproviderfactories
online-compilation
whoosh
php-parser
invite
locationlistener
web-application-design
gamequery
ti-dsp
temporal-database
procedural-music

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App