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 :)
omarkkkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Farzad AkbarnejadDeveloperCommented:
Hello,
This piece of code I think helps you well:
package home.ee;

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(
				"D:/Work/workspace/EECode/inp.txt")));

		String line = null;
		while ((line = br.readLine()) != null) {
			String[] pt = line.split(" ");
			for (int i = 1; i <= pt.length; i++) {
				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;
				}
			}
		}

		br.close();
	}
}

Open in new window

-FA
omarkkkAuthor Commented:
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
System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);

Open in new window


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 ..
CEHJCommented:
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));
                }

Open in new window

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

omarkkkAuthor Commented:
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
System.out.println("1 arguments: " + pt[0]);

Open in new window

then to this line
System.out.println("2 arguments: " + pt[0] + " " + pt[1]);

Open in new window

and then to the final line
System.out.println("4 arguments: " + pt[0] + " " + pt[1] + " " + pt[2]+" "+pt[3]);

Open in new window


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]);

Open in new window



thanks ::
krakatoaCommented:
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;
				}
			}
			
	}
		
}

Open in new window

CEHJCommented:
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));
                }

Open in new window

omarkkkAuthor Commented:
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 !
omarkkkAuthor Commented:
Hi krakatoa,
the problem is the st array is not fixed but depends on the lines sent by anther method!
CEHJCommented:
I tired that and it gives me almost the same!
I fail to see how that is NOT exactly what is wanted and expected ...
krakatoaCommented:
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.
omarkkkAuthor Commented:
if I have this string 364, 667, 182, 664
I only want it to go to this line of code
	                    System.out.println("4 arguments: " + pt[0] + ", " + pt[1] +
	                        ", " + pt[2] + ", " + pt[3]);

Open in new window

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]);

Open in new window

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]);

Open in new window


not pass through all of the switch lines!

thanks ...
krakatoaCommented:
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.
CEHJCommented:
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
Codrut TambuSr. Software Developer EngineerCommented:
There is an useless loop inside the code -> and that makes reading every line once again and generating additional output.
There's no reason to make the for loop.
so just replace the while with this one:
        while ((line = br.readLine()) != null) {
            String[] pt = line.split(" ");
            int i = pt.length;
            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]);
                    break;

                default: //...
                    System.out.println("neither case from above ");
            }
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Oops - yes i see what you mean - there IS a problem
Codrut TambuSr. Software Developer EngineerCommented:
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]);
CEHJCommented:
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();
    }
}

Open in new window

krakatoaCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.