Not sure if my mind is just numb or I am just stupid, but for some reason the logic behind this escapes me. Essentially in an android app I am pulling CSV information through a PHP script. That information gets displayed like
value00,value01,value02,value03,value04;value10,value11,value12,value13,value14;value20, etc...
now I want to set up a two dimensional array where thisArray[0][0] = value00, thisArray[1][1] = value11, thisArray[1][4] = value14, etc. The code I have will split by the ";" but I can't figure out how to then split that array into a two dimensional array set up the way I want. This is what I have: (httpMessage is the string containing the above information)
String[][] secondSplit;//
String[] firstSplit;//
String currvalue;//
firstSplit = httpMessage.split(";");
for(int i=0; i<firstSplit.length; i++) {
Log.d("EFFs" + Integer.toString(i),firstSplit[i]);
}
LogCat shows the desired behavior, EFFs0 = line 1, EFFs1 = line 2, just the way I want it. But now, how do I get that second dimension? Also, since I am 100% sure this is a dumb question with an easy answer I'll throw in another, is there an easy way to tell if a string is a number?
You could do the following:
secondSplit = new String[firstSplit.length][];
for(int i=0; i<firstSplit.length; i++){
secondSplit[i] = firstSplit[i].split(",");
}
Pretty sure that would work. Let me know if it doesn't! Hope that helps!
If you know how many rows and columns there are you can use the Scanner class like this (recently learned this from this answer):
Scanner sc = new Scanner(input).useDelimiter("[,;]");
final int M = 5;
final int N = 2;
String[][] matrix = new String[M][N];
for (int r = 0; r < M; r++) {
for (int c = 0; c < N; c++) {
matrix[r][c] = sc.next();
}
}
//Just to see that it worked
for(String[] row : matrix) {
for(String col : row) {
System.out.print(col+",");
}
System.out.println();
}
Related
I would like to know how array[array[i]]++ works in java.
I wrote the code, and want to know how this count integer array is working here
int[] counts = new int[201];
for (int i = 0; i < d; i++) {
counts[array_inside[i]]++;
}
and
also would like to know if i does like below how count array values will be written and left or right shift its values
for(int i = j; i < array_inside.length; i++){
count[array_inside[i-j]]--;
count[array_inside[i]]++;
}
Consider it as two operations (because it is). This
counts[array_inside[i]]++;
is equivalent to
int p = array_inside[i];
counts[p]++;
How to achieve the following. Here is a sample text file..
AH
1
2
3
BH
21
BT03
CH
CT02
AT10
This is a sample text file. I need read each line and find the count.. For example, there are 10 Lines within A record.. And 3 lines within B record and two lines within C Record.
How do I get this count. H-Header and T-Trailer. You may say, read the T record to find the count, but that count might be wrong. Hence I am trying to find the count and update the trailer records correctly.
I cannot upload the Java code I have written as I'm on mobile now.. Any suggestions is highly appreciated
Thanks
I'm not sure if I understand your question, you just want to count lines in a file? Try something like this:
Path path = Paths.get("yourFile.txt");
long lineCount = Files.lines(path).count();
use below code and read all file in array.
String[] stringArray = new String[10];
int length = stringArray.length;
for (int i = 0; i < length; i++) {
String stringI = stringArray[i];
if (stringI.contains("H")) {
// Head rec
String headRecName = stringI.replace("H", "");
int count = 1;
for (int j = i + 1; j < length; j++) {
count++;
String stringJ = stringArray[j];
if (stringJ.contains("T") && headRecName.equals(stringJ.replace("T", ""))) {
stringArray[j] = stringArray[j] + count;
}
}
}
}
}
I'm a newbie coder and in need some help. Im trying to add the digits of a number together according to a number corresponding to how many digits I can use. Im using Eclipse Juno and using a separate text file for using my numbers. Though it isn't much This is what I have now:
public static void main(String[] args) throws IOException{
String token1 = "";
Scanner infile1 = new Scanner(new File("input.txt"));
ArrayList<String> temps = new ArrayList<String>();
//Adding Input files to the Arraylist
while(infile1.hasNext()){
token1 = infile1.next();
temps.add(token1);
}
infile1.close();
//Calling the Numbers from ArrrayList temps
for(int x = 0; x < temps.size(); x++) {
System.out.println(x + " " + temps.get(x));
for(x = 0; x < temps.size(); x++ ){
}
}
}
}
The numbers are
9678415 7,
9678415 6,
9678415 5,
9678415 4,
2678515 3,
Number to add, digits to use. The input.txt file does not have commas
Exactly how you do this is going to depend on what the data looks like in the file. You say it's not delimited by commas so I will have to assume they are separated by lines. You will need to separate the values within the strings and convert to int; so the below should do what you are attempting, if I understand the question. (full disclosure, it's been a little while since I've written in Java and I don't have a way to test this right now, make sure I haven't made any basic syntax errors)
ArrayList<Integer> totalArray = ArrayList<Integer>();
for(String tStr : temps){
int tempTotal = 0;
String[] numArray = tStr.split(" ");
for(int x = 0; x < Integer.parseInt(numArray[1]){
int y = Integer.parseInt(numArray[0].substring(x,x+1));
tempTotal += y;
}
totalArray.add(tempTotal);
}
There are probably better ways to get the highest value, but since I have been out of this for a little while, I'm just going to do it in the most basic way I can think of.
int highestValue = 0;
for (Integer x : totalArray){
if(highestValue<=x){
highestValue = x;
}
}
return highestValue;
I'm very new to programming. I'm studying on my own, and the examples from the book I'm using are too easy so I'm trying to do something harder. Lotteries are one of my hobbies and I think the problem I chose will make it easier for me to learn Java.
This program calculates frequencies (how many times each number from 1 t0 70 appears in my txt file) in Keno, a type of lottery(In Keno, a draw consists of 20 numbers out of 70, instead of the widespread 6 out of 49).
I want to calculate the frequencies not for the entire txt file, but just for a section of it, for example if the file has x lines, I want just the lines between x-5 and x-10, something like this.I don't know the number of lines in my file, perhaps thousands, but it always has 20 columns.
The program works fine for the entire file, but I run into trouble when trying to work just on a section of it. I think that I should read the file into a 2d array and then I could process the lines I want. I'm having hard times transferring every line into a matrix. I've read every post regarding reading a file into a 2d array but couldn't make it work.
Below is one of the many attempts I made over more than a week
public static void main(String args[]) {
int[][] matricea = new int [30][40];
int x=0, y=0;
try {
BufferedReader reader = new BufferedReader(
new FileReader("C:\\keno.txt")
);
int[] numbers = new int[72]; //each keno draw has 70 numbers
for (int i = 0; i < 71; i++ ){
numbers[i] = 0;
}
int k=0; // k counts the lines
String draw;
while ( (draw = reader.readLine()) != null ) {
String[] pieces = draw.split(" +");
k++;
for (String str : pieces) {
int str_int = Integer.parseInt(str);
matricea[x][y] = str_int;
System.out.print(matricea [x][y] + " ");
y = y + 1;
}
x = x + 1;
System.out.println(" ");
}
for (int j = 1; j <= 20; j++) {
int drawnNumber = Integer.parseInt(pieces[j]);
numbers[drawnNumber]++;
}
System.out.println(" nr. of lines is " + k);
reader.close();
for (int i = 0; i < 71; i++) {
System.out.println(i + ": " + numbers[i]);
}
} catch (Exception e) {
System.out.println("There was a problem opening and processing the file.");
}
}
Formatting and improving current code
I'll show how to solve your problem, but first I would like to point out a few things in your code that could be improved by some formatting, or that perhaps are unnecessary.
Just to help you improve your coding skills, readability is very important! :)
And don't forget, consistency is key! If you like one style better than the more common style, or the preferred style, that's fine as long as you use it throughout your coding. Don't switch between two styles.
If you don't care to read these comments, you can find the solution at the bottom of my answer. Just note that your original code will be different in my solution because I have formatted it to be most readable for me.
Spacing in variable declarations
Original code
int[][] matricea = new int [30][40];
int x=0, y=0;
Spacing modified
int[][] matricea = new int[30][40];
int x = 0, y = 0;
Notice the space removed between int and [30][40], and the space added between the variables and the initialization, i.e. - x=0 => x = 0.
Initializing an int array to contain all 0's
Original code
int[] numbers = new int[72]; //each keno draw has 70 numbers
for (int i = 0; i < 71; i++ ){
numbers[i] = 0;
}
Same as
int[] numbers = new int[72]; //each keno draw has 70 numbers
You don't have to set each value to 0, Java will do that for you. In fact, Java has default values, or null values, for all types. Thanks to Debosmit Ray!
I won't go into the exceptions to this case, or when or why or how, you can read about that in this post, and pay close attention to Aniket Thakur's answer.
But why do you have an array of size 72, if there are only 70 possibilities?
Choosing variable names
Original code
int k=0; // k counts the lines
Same as
int numLines = 0;
You should always make your variables names something meaningful to their purpose. If you ever have to put a comment like k counts the lines to describe a variable's purpose, consider if a better name would work instead.
Functionalizing code
Original code
while ( (draw = reader.readLine()) != null ) {
String[] pieces = draw.split(" +");
k++;
for (String str : pieces) {
int str_int = Integer.parseInt(str);
matricea[x][y] = str_int;
System.out.print(matricea [x][y] + " ");
y = y + 1;
}
x = x + 1;
System.out.println(" ");
}
Same as
while ( (draw = reader.readLine()) != null ) {
processLine(draw);
}
Of course, you'll have to make the method processLine(String line), but that won't be hard. It's just taking what you have and moving it to a separate method.
The original code's while loop is very busy and messy, but with the latter option makes the purpose clear, and the code clean.
Of course each situation is different, and you might find that only removing part of the code into a method would be a better solution. Just play around and see what makes sense.
Error!
Original code
for (int j = 1; j <= 20; j++) {
int drawnNumber = Integer.parseInt(pieces[j]);
numbers[drawnNumber]++;
}
This code should not work, since pieces is declared in the while loop above it, and is local to that above loop. This for loop is outside the scope of where pieces exists.
I'll tell you how to fix it, but I'm not sure what the code is supposed to be doing. Just let me know what its purpose is, and I'll provide you with a solution.
After formatting!
This is what the code may look like after applying my above comments. I have added comments to parts that I have changed.
public static void main(String args[]) {
try {
doKenoStuff();
} catch(IOException e) {
System.out.println("There was a problem opening and processing the file.");
}
}
public static void doKenoStuff() throws IOException {
BufferedReader reader = new BufferedReader(
new FileReader("C:\\keno.txt")
);
int[][] matricea = new int[30][40];
int[] numbers = new int[72]; //each keno draw has 70 numbers
// We can clean up our loop condition by removing
// the assignment (draw = reader.readLine) from it.
// Just make sure draw doesn't begin as null.
String draw = "";
int row;
for(row = 0; draw != null; row++) {
draw = reader.readLine();
// We read a line from the file, then send it
// to extractLineData which will collect the info
// from each column, and update matricea and numbers
extractLineData(draw, row, matricea, numbers);
}
System.out.println("Number of lines: " + row);
System.out.println("Each number's drawing stats:");
for (int i = 0; i < 71; i++) {
System.out.println(i + ": " + numbers[i]);
}
reader.close();
}
public static void extractLineData(String line, int row, int[][] matrix, int[] numbers) {
String linePieces = line.split(" +");
for(int column = 0; column < linePieces.length; column++) {
int number = Integer.parseInt(linePieces[column]);
matrix[row][column] = number;
numbers[number]++;
}
}
Solution
Note: I am not perfect, and my code is not either. I'm not trying to say that what I have suggested is in any way the only way to do this. It could definitely be improved, but it is a start. You should take my solution and see how you can improve it yourself.
What can you find that you could code in a cleaner, or faster, or better way?
So, how do we fix the problem?
We have a method that reads from the start of a file, to the end of it, and it logs the data it finds inside matricea.
A quick and easy solution is to simply make that method take in two parameters, a starting line number, and an ending line number.
public static void doKenoStuff(int start, int end) throws IOException {
Then we simply make a loop to skip over the starting lines! It's that easy!!!
for(int i = 0; i < start - 1; i++) {
reader.readLine();
}
Don't forget that we may not need the big 30 row matricea to be 30 rows anymore. We can shrink that down to end - start + 1. That way, if a user wants to read from line 45 to line 45, we only need 45 - 45 + 1 = 1 row in matricea.
int[][] matricea = new int[end - start + 1][40];
And the very last thing we need to add is a condition in our line reading loop, that prevents us from going past the ending line.
for(row = 0; draw != null, row <= end; row++) {
And there you have it. Simple as that!
Complete solution
public static void main(String args[]) {
int start = 7, end = 18;
try {
doKenoStuff(start, end);
} catch(IOException e) {
System.out.println("There was a problem opening and processing the file.");
}
}
public static void doKenoStuff(int start, int end) throws IOException {
BufferedReader reader = new BufferedReader(
new FileReader("C:\\keno.txt")
);
int[][] matricea = new int[end - start + 1][40];
int[] numbers = new int[72]; //each keno draw has 70 numbers
for(int i = 0; i < start - 1; i++) {
reader.readLine();
}
String draw = "";
int row;
for(row = 0; draw != null, row <= end; row++) {
draw = reader.readLine();
extractLineData(draw, row, matricea, numbers);
}
System.out.println("Number of lines: " + row);
System.out.println("Each number's drawing stats:");
for (int i = 0; i < 71; i++) {
System.out.println(i + ": " + numbers[i]);
}
reader.close();
}
public static void extractLineData(String line, int row, int[][] matrix, int[] numbers) {
String linePieces = line.split(" +");
for(int column = 0; column < linePieces.length; column++) {
try {
int number = Integer.parseInt(linePieces[column]);
matrix[row][column] = number;
numbers[number]++;
} catch (NumberFormatException) {
// You don't have to do anything in this block, but
// you can print out what input gave the exception if you want.
System.out.println("Bad input: \"" + linePieces[column] + "\"");
}
}
}
I will have a series of random arrays similar to.
array1[] = {1,2,3,0,0,5,6}
array1[] = {1,2,0,0,4,5,6}
I want them to end up like, so I replace the first 0 with X.
array1[] = {1,2,3,X,0,5,6}
array1[] = {1,2,X,0,4,5,6}
the code I'm using replaces all zeroes giving instead of just one.
array1[] = {1,2,3,X,X,5,6}
array1[] = {1,2,X,X,4,5,6}
Which isn't what I'm looking for. I'd be happy just replacing either one but only one.
The code I'm using,
for(int i=0; i<array.length; i++){
if(fruit[i] == 0)
fruit[i]=X;
}
Hope that was clear, thanks for any help! Being stuck at this for a little while now.
Try using break.
for(int i = 0; i < array.length; i++) {
if(fruit[i] == 0) {
fruit[i] = X;
break;
}
}
This will ensure only one is changed, max.