java - Save multiple strings into one string -



java - Save multiple strings into one string -

i want save multiple strings in one. thing is, don't know how many strings may be. i'm creating programme reads calories text file , stores them in corresponding arrays.

here parts of text:

description of nutrient fat nutrient energy carbohydrate protein cholesterol weight saturated fat (grams) (calories) (grams) (grams) (milligrams) (grams) (grams) apples, raw, peeled, sliced 1 cup 0 65 16 0 0 110 0.1 apples, raw, unpeeled,2 per lb1 apple 1 125 32 0 0 212 0.1 apples, raw, unpeeled,3 per lb1 apple 0 80 21 0 0 138 0.1 apricot nectar, no added vit c1 cup 0 140 36 1 0 251 0

now nutrient name, have array foodname. read whole string until reach int amount.

here i've done far:

scanner input = new scanner("calories.txt"); while (input.hasnext()) { string[] words = input.next().split(" "); int lasti; (int i=0; < words.length; i++) { if (isnumeric(words[i])) { lasti = i; for(int j=lasti; j>=0; j++){ //what should set here? } } } } public static boolean isnumeric(string str) { seek { double d = double.parsedouble(str); } grab (numberformatexception nfe) { homecoming false; } homecoming true; }

for inner loop, kept track of lastly index start , go backwards.

problem 1: if go backwards in sec line, re-create both lines.

problem 2: how save strings of name in 1 index of foodname?

all help appreciated :)

what looking in java called stringbuilder. can utilize string , maintain appending onto it.

file file = new file("output.txt"); scanner input = new scanner(file); stringbuilder sb = new stringbuilder(); while (input.hasnextline()) { string[] words = input.nextline().split(" "); (int = 0; < words.length; i++) { if (isnumeric(words[i])) { break; } sb.append(words[i] + " "); } system.out.println(sb); sb = new stringbuilder(); } input.close();

what read file line line, creating array of strings splitting line on " ". then, iterates on each of strings in array , checks if number, if is, break current loop , move onto next line.

i had stringbuilder print after each line, , reset, should replace whatever functionality want.

a couples suggestions you:

use csv file. separate commas instead of spaces, makes parsing extremely easy. use regex check if string number instead of catching exceptions, more elegant.

the output of comes out little funny because of how formatted file. parsing on " ", added bunch of " " characters in file create format nice. messes parsing badly. but, method parse correctly when prepare format of flat file.

output was: (note each line separate string. can see how file formatting messed output)

description of nutrient fat nutrient energy carbohydrate protein cholesterol weight saturated fat (grams) (calories) (grams) (grams) (milligrams) (grams) (grams) apples, raw, peeled, sliced apples, raw, unpeeled,2 per lb1 apple apples, raw, unpeeled,3 per lb1 apple apricot nectar, no added vit c1 cup

java string file

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -