1

I have a txt file like this

163 Money Drop 24

454 Happy 112

45 Nothing Without You 56

The first number is the id of the song which I want to store it as an integer, the second is a String !That is not one word! and the third is an integer which is the number of likes of the song. I want to store them in an array but when I use the scanner method I store them word by word and when a song has a name > 1 word I have a problem. Here's my testing code

public void readFile() {
    while(x.hasNext()){
        Song test = new Song(Integer.parseInt(x.next()), x.next(), Integer.parseInt(x.next()));
    }
}

Is there any way I can read the txt file, store the first number as an int and then scan for the whole string(name of the song) until the next element is a number?

Community
  • 1
  • 1
vkatsou
  • 73
  • 6
  • 2
    What happens when the number is part of the song name? – f1sh Dec 07 '18 at 13:44
  • There's not such a thing in my case – vkatsou Dec 07 '18 at 13:46
  • You could read the while line and then parse it using a regex, but if you can still change the format of the text file, it would be better to have a dedicated separator character like ',' or ';' between your fields (i.e. turn your textfile in a *.csv, this also makes it easy to open it in a spreadsheet program like excel). – Hulk Dec 07 '18 at 13:51
  • Why don't you simply read the line in `str`, parse as int the `substring(0, firstIndexOf whitespace)`, parse as int the `substring(lastIndexOf whitespace)`, and then use substr(firstIndexOf, lastIndexOf)` as a title? – dbl Dec 07 '18 at 13:51

2 Answers2

1

The easiest way without regex is probably to use the scanner to read in the single words of your title and then combine them to create the name. Something like this:

int id = myScanner.nextInt();
String title = myScanner.next();

//while title has more words
while(!myScanner.hasNextInt()){
   //append words with whitespace
   title = title + " " + myScanner.next();
}

int likes = myScanner.nextInt();
vkatsou
  • 73
  • 6
Mr.Yellow
  • 692
  • 5
  • 15
  • I like the idea. One suggestion, instead of concatenation it is better to use StringBuilder to get better performance. More info: [Why StringBuilder when there is String?](https://stackoverflow.com/q/5234147) – Pshemo Dec 07 '18 at 14:02
  • It's wrong because maybe the title has int value for example 444 50 cent 323 – Mohsen Dec 07 '18 at 14:03
  • @Spara According to first two comments: "(1) What happens when the number is part of the song name? -> (2 - OP) There's not such a thing in my case". – Pshemo Dec 07 '18 at 14:05
  • You can extend the code so that it appends even the numbers in case that there is another number after the number we just read – Mr.Yellow Dec 07 '18 at 14:08
  • @Pshemo but in general it's better to consider all situations and the code should not get exception on invalid inputs – Mohsen Dec 07 '18 at 14:12
  • while(!myScanner.hasInt()) Intellij IDEA says cannot find method hasInt – vkatsou Dec 07 '18 at 14:13
  • 1
    @user3699548 You are looking for `hasNextInt()` in this case. – Pshemo Dec 07 '18 at 14:16
  • Tried that and didn't work it stops when a song has a title > 1 word – vkatsou Dec 07 '18 at 14:17
0

Because there is no separator character, I assume that the first word is the id and the last word is number of likes and the others on mid is the name. So you can use this method:

String line = scanner.nextLine();
        String[] slices = line.split(" ");
        Song test = new Song(Integer.parseInt(slices[0]), line.substring(slices[0].length() + 1, line.lastIndexOf(" ")), slices[slices.length - 1]);
Mohsen
  • 4,536
  • 2
  • 27
  • 49