omarkkk
asked on
reading string java
Hi,
I have a text file generated by the application that contains integers like this:
26
50 597 424 561
499 849
283 76
115 81
4
26
What I want to do is reading this integers, but in a different way.
I know how to read the text file and display it, but what I want is make a decision about the numbers so:
if it is in one position without spaces like 26 send it to the right method as argument - as I have ready methods-.
if it is in two positions with one space like 499 849 send it the right method.
and so one,
is there a way to do this ?
I mean distinguishing between the numbers .
thanks :)
I have a text file generated by the application that contains integers like this:
26
50 597 424 561
499 849
283 76
115 81
4
26
What I want to do is reading this integers, but in a different way.
I know how to read the text file and display it, but what I want is make a decision about the numbers so:
if it is in one position without spaces like 26 send it to the right method as argument - as I have ready methods-.
if it is in two positions with one space like 499 849 send it the right method.
and so one,
is there a way to do this ?
I mean distinguishing between the numbers .
thanks :)
ASKER
Hi Farzad,
this one is working but needs little modifications I this, because it's giving me this out put :
26
1 arguments: 26
456 660 136 645
1 arguments: 456
2 arguments: 456 660
3 arguments: 456 660 136 645
408 614
1 arguments: 408
2 arguments: 408 614
4
1 arguments: 4
391 494
1 arguments: 391
2 arguments: 391 494
4
1 arguments: 4
26
1 arguments: 26
As you can see
the third line for example is 456 660 136 645 but I don't want to add the first then the second as it does! I want it to add this 456 660 136 645 directly to this line of code
also with the two arguments like this 391 494 it adds first the 391 and then add them both to the 2 arguments!
any idea how to make them directly match with the arguments ??
thanks for replay ..
this one is working but needs little modifications I this, because it's giving me this out put :
26
1 arguments: 26
456 660 136 645
1 arguments: 456
2 arguments: 456 660
3 arguments: 456 660 136 645
408 614
1 arguments: 408
2 arguments: 408 614
4
1 arguments: 4
391 494
1 arguments: 391
2 arguments: 391 494
4
1 arguments: 4
26
1 arguments: 26
As you can see
the third line for example is 456 660 136 645 but I don't want to add the first then the second as it does! I want it to add this 456 660 136 645 directly to this line of code
System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);
also with the two arguments like this 391 494 it adds first the 391 and then add them both to the 2 arguments!
any idea how to make them directly match with the arguments ??
thanks for replay ..
Farzad's code works perfectly well with the input file on the whole. The following case statement at the end will catch all:
default:
System.out.printf("%d arguments: %s%n",
pt.length, java.util.Arrays.toString(pt));
}
ASKER
Hi CEHJ,
it's working as I said, but there is a duplication in displaying as you can see post after testing the Farzad's code!
if I have the string
50 597 424 561
I need to match them directly to the this line System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);
but as you can see, passes first to this
which make duplication !!
is there any way so if this string 50 597 424 561 matched directly to this one
thanks ::
it's working as I said, but there is a duplication in displaying as you can see post after testing the Farzad's code!
if I have the string
50 597 424 561
I need to match them directly to the this line System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);
but as you can see, passes first to this
System.out.println("1 arguments: " + pt[0]);
then to this lineSystem.out.println("2 arguments: " + pt[0] + " " + pt[1]);
and then to the final lineSystem.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);
which make duplication !!
is there any way so if this string 50 597 424 561 matched directly to this one
System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);
thanks ::
public class QA01 {
static String[] st = {"26","50 597 424 561","499 849","283 76","115 81","4","26"};
public static void main(String[] args) throws IOException {
String[] sa;
for (int i = 1; i <= st.length; i++) {
switch ((sa=(st[i-1].split(" "))).length) {
case 1:
System.out.println("1 arguments: " + sa[0]);
break;
case 2:
System.out.println("2 arguments: " + sa[0] + " " + sa[1]);
break;
case 3:
System.out.println("3 arguments: " + sa[0] + " " + sa[1] + " " + sa[2]);
break;
case 4:
System.out.println("4 arguments: " + sa[0] + " " + sa[1] + " " + sa[2]+ " " + sa[3]);
break;
}
}
}
}
Here's what the case statement should be
switch (i) {
case 1:
System.out.println("1 arguments: " + pt[0]);
break;
case 2:
System.out.println("2 arguments: " + pt[0] + ", " + pt[1]);
break;
case 3:
System.out.println("3 arguments: " + pt[0] + ", " + pt[1] +
", " + pt[2]);
break;
case 4:
System.out.println("4 arguments: " + pt[0] + ", " + pt[1] +
", " + pt[2] + ", " + pt[3]);
break;
default:
System.out.printf("%d arguments: %s%n", pt.length,
java.util.Arrays.toString(pt));
}
ASKER
Hi CEHJ,
I tired that and it gives me almost the same!
here is the ouput:
1 arguments: 26
1 arguments: 364
2 arguments: 364, 667
3 arguments: 364, 667, 182
4 arguments: 364, 667, 182, 664
1 arguments: 271
2 arguments: 271, 729
3 arguments: 271, 729, 348
4 arguments: 271, 729, 348, 580
1 arguments: 134
2 arguments: 134, 652
3 arguments: 134, 652, 439
4 arguments: 134, 652, 439, 674
1 arguments: 261
2 arguments: 261, 876
1 arguments: 4
1 arguments: 26
any idea please how to match directly !
I tired that and it gives me almost the same!
here is the ouput:
1 arguments: 26
1 arguments: 364
2 arguments: 364, 667
3 arguments: 364, 667, 182
4 arguments: 364, 667, 182, 664
1 arguments: 271
2 arguments: 271, 729
3 arguments: 271, 729, 348
4 arguments: 271, 729, 348, 580
1 arguments: 134
2 arguments: 134, 652
3 arguments: 134, 652, 439
4 arguments: 134, 652, 439, 674
1 arguments: 261
2 arguments: 261, 876
1 arguments: 4
1 arguments: 26
any idea please how to match directly !
ASKER
Hi krakatoa,
the problem is the st array is not fixed but depends on the lines sent by anther method!
the problem is the st array is not fixed but depends on the lines sent by anther method!
I tired that and it gives me almost the same!I fail to see how that is NOT exactly what is wanted and expected ...
the problem is the st array is not fixed but depends on the lines sent by anther method!
. . . yes, I know. But instead of me going to the length of creating a File and reading it back in, I just put the strings into a string array for convenience here.
You have to ensure that when you are reading the lines in from your file, that the lines *are* being read in integrally, and that, for instance, you capture something like "456 789 100" in a single string, and not that it arrives as three separate ones.
ASKER
if I have this string 364, 667, 182, 664
I only want it to go to this line of code
4 arguments: 364, 667, 182, 664
what it does now, is passing the first one 364 to this line
1 arguments: 364
NOTE:
this line 364 667 182 664 is one line inside my List, and the needed is just to place it in the correct switch line which is
not pass through all of the switch lines!
thanks ...
I only want it to go to this line of code
System.out.println("4 arguments: " + pt[0] + ", " + pt[1] +
", " + pt[2] + ", " + pt[3]);
and print this "only"4 arguments: 364, 667, 182, 664
what it does now, is passing the first one 364 to this line
System.out.println("1 arguments: " + pt[0]);
and prints 1 arguments: 364
NOTE:
this line 364 667 182 664 is one line inside my List, and the needed is just to place it in the correct switch line which is
System.out.println("4 arguments: " + pt[0] + ", " + pt[1] +
", " + pt[2] + ", " + pt[3]);
not pass through all of the switch lines!
thanks ...
this line 364 667 182 664 is one line inside my List,
right so now you are talking about a List being involved, no mention of that was made before. Maybe it would be better if you posted your code so that we can have a look at what's going on rather than guessing continually.
I only want it to go to this line of code
But it DOES do that. It's completely independent of any other array passed. If your file contained only one line of four elements, only that line would print, and nothing else
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops - yes i see what you mean - there IS a problem
sorry I've had a typo, forgot the forth argument on that line:
case 4:
System.out.println("4 arguments: " + pt[0] + ", " + pt[1] + ", " + pt[2]", " + pt[3]);
case 4:
System.out.println("4 arguments: " + pt[0] + ", " + pt[1] + ", " + pt[2]", " + pt[3]);
You probably want
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class QA01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(
new File("inp.txt")));
String line = null;
while ((line = br.readLine()) != null) {
String[] pt = line.split(" ");
switch (pt.length) {
case 1:
System.out.println("1 arguments: " +
java.util.Arrays.toString(pt));
break;
case 2:
System.out.println("2 arguments: " +
java.util.Arrays.toString(pt));
break;
case 3:
System.out.println("3 arguments: " +
java.util.Arrays.toString(pt));
break;
case 4:
System.out.println("4 arguments: " +
java.util.Arrays.toString(pt));
break;
default:
System.out.printf("%d arguments: %s%n", pt.length,
java.util.Arrays.toString(pt));
}
}
br.close();
}
}
There is an useless loop inside the code
which is essentially exactly what I was saying here.
You were effectively reading in the strings using the space character as an end of string marker.
This piece of code I think helps you well:
Open in new window
-FA