-3

I have the following code that reads a file that has 3 integers in the formats of a single number, or x/y where x is an int as is y.

I'm trying to read the file, then split at each white space in order to isolate each part of the string to end up using "parseInt" to get them into integers. So far I have

BufferedReader br = new BufferedReader(new FileReader("input1.txt"));

            String line = br.readLine();

            while (line != null){
                String[] lines = line.split(" ");

I assume after this it should look something like this as an example

lines[0] = 4

lines[1] = 4/2

Looking at this I assume I can parse each part using lines[1] etc

So my Question is how I can check for a "/" since i won't be able to use parseInt if it has a dash. I assumed something like and if statement, if lines has "/" etc... but i wasn't sure of the Syntax,

Any help would be appreciated,

Regards,

James

Sim
  • 570
  • 1
  • 10
  • 22

5 Answers5

3

Foreach element in the lines array check to see if the String contains a "/" using the String.Contains() method. If this returns true, then perform another split using "/" as a delimiter. You will then have an array of an x and y value which can be parsed to an integer.

Phil Applegate
  • 567
  • 2
  • 9
  • No problem - you could of course perform this second split when you loop through each line of your text file. Saves having to do the second foreach loop. You may well have already done this :) – Phil Applegate Apr 19 '13 at 11:58
  • anychance you could tell me why so many downvotes, I actually read posting guidelines and tried to make it decent enough -.-' – Sim Apr 19 '13 at 12:00
0

There is this method, that will help you: String.indexOf()

    int slashIndex = line.indexOf('/');
    if (slashIndex != -1) {
        double x = Double.parseDouble(line.substring(0, slashIndex));
        double y = Double.parseDouble(line.substring(slashIndex + 1));
        double value = x / y;
        System.out.println(line + " = " + value); // or else...
    }
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
0

Try this:

String line ="";

while ((line=br.readLine())  != null){
    if(line.contains("/")){
        String[] lines = line.split("/");
        //parse 
    }
    else{ 
        String[] lines = line.split(" ");
        //parse 
    }
}
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

You do not need to check you can just use replace or replaceAll directly:

str.replaceAll("/", "");

By the way your loop will not work as is, you need to call the br.readLine() for each round:

while ((line = br.readLine()) != null){
  for (String ln: line.split(" ")){
    try{
      anyIntVar = Integer.parseInt(ln.replaceAll("/", ""));
      // ...
    }catch (NumberFormatException nfe){
      // handle exception
    }
  }
}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

You can use contains method to check if the line contain "/" or not , and if you find it you can split this line by using "/" Like this :

String s = "4/2";
if (s.contains("/")) {
   String[] split = s.split("/");
   int x = Integer.parseInt(split[0]);
   int y = Integer.parseInt(split[1]);

}
Alya'a Gamal
  • 5,624
  • 19
  • 34