Pokecallum
Pokecallum

Reputation: 33

Reading int, double and strings and ignore tokens

12.9 ; I @ 13 jav

3.8 ; can @ 6 aru

4.0 ; read @ 109 les

The program is supposed to read this in as a string and then add all the doubles, integers, and then to add the first string together and the last string together. So the program should provide

Double total : 20.7

Integer total : 128

Line : I can read

Word: javarules

This is what I have so far , I know that I have to use scanner to skip over the tokens .

public class Test {
public static void main (String args[]) throws FileNotFoundException {
    Scanner sc = new Scanner(Test4.class.getResourceAsStream("week2inputdata.txt"));

    double doubletotal = 0;
    int inttotal = 0; 
    String line = " ";
    String word;

    Scanner scanLine;
    while (sc.hasNextLine()){
      scanLine = new Scanner (sc.nextLine());
      scanLine.next();

      line += sc.hasNextLine();   
      inttotal += sc.nextInt();
      // scanLine = new Scanner (sc.nextLine());
      // scanLine.next();
      // line += sc.next() + " ";
      // inttotal += sc.nextInt();
      doubletotal += sc.nextDouble();
    }
        System.out.println(inttotal);
        System.out.println(doubletotal);
        System.out.println(line);
    }
}

Upvotes: 1

Views: 1136

Answers (2)

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

This is rather ugly but it will work,

String[] weirdLookingString = new String[] { "12.9 ; I @ 11 jav", "3.8 ; can @ 11 aru"
                                            ,"4.0 ; read @ 109 les" };

double doubleValue = 0.0;
String strValue1 = "";
String strValue2 = "";
int integerValue = 0;

for (int i = 0; i < weirdLookingString.length; i++) {
    String array[] = weirdLookingString[i].split("[;\\@]\\s+");
    String lastString[] = array[2].split("\\s");
    integerValue += Integer.parseInt(lastString[0]);
    doubleValue += Double.parseDouble(array[0]);
    strValue2 += lastString[1];
    strValue1 += array[1];
}

System.out.println("Integer value: " + integerValue);
System.out.println("Double value: " + doubleValue);
System.out.println("Words: " + strValue2);
System.out.println("Line: " + strValue1);

output,

Integer value: 131
Double value: 20.7
Words: javarules
Line: I can read 

Upvotes: 3

Coding Enthusiast
Coding Enthusiast

Reputation: 3933

You can create an arraylist of double, Integers and then create a sb builder for the first set of string and a stringBuilder for the second set of strings.

String line = "12.9 ; I @ 13 jav";
String str[] = line.split("[;\\@\\s+]");
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
ArrayList<Double> doubleArray = new ArrayList();
ArrayList<Integer> intArray = new ArrayList();

for(int i =0; i < str.length; i++){
    if(i == 0){
       doubleArray.add(Double.parseDouble(str[i]));//convert to double and add it to the list
    }else if(i == 1){
        sb.append(str[i]);
    }else if(i == 2){
       doubleInt.add(Integer.parseInt(str[i]));//convert it to integer and add to the list
    }else if(i == 3){
       sb2.append(str[i]);
    } 
}

you can put everything in a loop so you won't have to rewrite everything. after everything has been added you can build a string using the stringBuilder and add everything using the array from arrayList

Upvotes: 0

Related Questions