How do I get certain data within a text file using arrays?
I have a text file (Items.txt) that has the following data: date, name, description and price and its formatted like so:
31/10/2018, Food, Hamburger, 10.00
31/10/2018, Clothes, Hoodie, 50.00
1/11/2018, Games, Controller, 150.00
1/11/2018, Food, Chips, 5.00
How would I get the total amount of the the food only? Here is the snippet I have worked so far:
public static double totalFoodExpense(byte choiceCode, byte expenseChoiceCode, doubletotalExpense) throws IOException{
double total =0;
double amount = 0;
String expense= "";
xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("Items.txt"));
System.out.println("Enter the expense code to determine the total amount that you have spent on");
System.out.println("Codes < Food(1), Clothes(2), Games(3), > n Please " +
"just enter a number");
xpenseCode = Byte.parseByte(kb.nextLine());
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
if(xpenseCode == 1){
//Nothing yet
}
}
return total;
}
The user would enter a number( 1 for Food, 2 for clothes, and 3 for games) to determine the total amount of that certain item. So an expected output would look like this:
Enter the expense code to determine the total amount that you have spent on
Codes < Food(1), Clothes(2), Games(3), > Please just enter a number:
1 <--user input
The total expense of Food is 15.00
java text
add a comment |
I have a text file (Items.txt) that has the following data: date, name, description and price and its formatted like so:
31/10/2018, Food, Hamburger, 10.00
31/10/2018, Clothes, Hoodie, 50.00
1/11/2018, Games, Controller, 150.00
1/11/2018, Food, Chips, 5.00
How would I get the total amount of the the food only? Here is the snippet I have worked so far:
public static double totalFoodExpense(byte choiceCode, byte expenseChoiceCode, doubletotalExpense) throws IOException{
double total =0;
double amount = 0;
String expense= "";
xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("Items.txt"));
System.out.println("Enter the expense code to determine the total amount that you have spent on");
System.out.println("Codes < Food(1), Clothes(2), Games(3), > n Please " +
"just enter a number");
xpenseCode = Byte.parseByte(kb.nextLine());
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
if(xpenseCode == 1){
//Nothing yet
}
}
return total;
}
The user would enter a number( 1 for Food, 2 for clothes, and 3 for games) to determine the total amount of that certain item. So an expected output would look like this:
Enter the expense code to determine the total amount that you have spent on
Codes < Food(1), Clothes(2), Games(3), > Please just enter a number:
1 <--user input
The total expense of Food is 15.00
java text
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
1
What exactly is the problem? All you have to do is to check if the second element ofparts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.
– Amongalen
Nov 26 '18 at 12:14
1
WhyByte.parseByte()
?
– Joakim Danielson
Nov 26 '18 at 14:08
add a comment |
I have a text file (Items.txt) that has the following data: date, name, description and price and its formatted like so:
31/10/2018, Food, Hamburger, 10.00
31/10/2018, Clothes, Hoodie, 50.00
1/11/2018, Games, Controller, 150.00
1/11/2018, Food, Chips, 5.00
How would I get the total amount of the the food only? Here is the snippet I have worked so far:
public static double totalFoodExpense(byte choiceCode, byte expenseChoiceCode, doubletotalExpense) throws IOException{
double total =0;
double amount = 0;
String expense= "";
xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("Items.txt"));
System.out.println("Enter the expense code to determine the total amount that you have spent on");
System.out.println("Codes < Food(1), Clothes(2), Games(3), > n Please " +
"just enter a number");
xpenseCode = Byte.parseByte(kb.nextLine());
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
if(xpenseCode == 1){
//Nothing yet
}
}
return total;
}
The user would enter a number( 1 for Food, 2 for clothes, and 3 for games) to determine the total amount of that certain item. So an expected output would look like this:
Enter the expense code to determine the total amount that you have spent on
Codes < Food(1), Clothes(2), Games(3), > Please just enter a number:
1 <--user input
The total expense of Food is 15.00
java text
I have a text file (Items.txt) that has the following data: date, name, description and price and its formatted like so:
31/10/2018, Food, Hamburger, 10.00
31/10/2018, Clothes, Hoodie, 50.00
1/11/2018, Games, Controller, 150.00
1/11/2018, Food, Chips, 5.00
How would I get the total amount of the the food only? Here is the snippet I have worked so far:
public static double totalFoodExpense(byte choiceCode, byte expenseChoiceCode, doubletotalExpense) throws IOException{
double total =0;
double amount = 0;
String expense= "";
xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("Items.txt"));
System.out.println("Enter the expense code to determine the total amount that you have spent on");
System.out.println("Codes < Food(1), Clothes(2), Games(3), > n Please " +
"just enter a number");
xpenseCode = Byte.parseByte(kb.nextLine());
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
if(xpenseCode == 1){
//Nothing yet
}
}
return total;
}
The user would enter a number( 1 for Food, 2 for clothes, and 3 for games) to determine the total amount of that certain item. So an expected output would look like this:
Enter the expense code to determine the total amount that you have spent on
Codes < Food(1), Clothes(2), Games(3), > Please just enter a number:
1 <--user input
The total expense of Food is 15.00
java text
java text
asked Nov 26 '18 at 12:06
Joey DeguzmanJoey Deguzman
406
406
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
1
What exactly is the problem? All you have to do is to check if the second element ofparts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.
– Amongalen
Nov 26 '18 at 12:14
1
WhyByte.parseByte()
?
– Joakim Danielson
Nov 26 '18 at 14:08
add a comment |
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
1
What exactly is the problem? All you have to do is to check if the second element ofparts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.
– Amongalen
Nov 26 '18 at 12:14
1
WhyByte.parseByte()
?
– Joakim Danielson
Nov 26 '18 at 14:08
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
1
1
What exactly is the problem? All you have to do is to check if the second element of
parts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.– Amongalen
Nov 26 '18 at 12:14
What exactly is the problem? All you have to do is to check if the second element of
parts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.– Amongalen
Nov 26 '18 at 12:14
1
1
Why
Byte.parseByte()
?– Joakim Danielson
Nov 26 '18 at 14:08
Why
Byte.parseByte()
?– Joakim Danielson
Nov 26 '18 at 14:08
add a comment |
1 Answer
1
active
oldest
votes
I have figured it out.
public static double totalTuitionExpense() throws IOException{
double total =0;
double amount = 0;
String expense= "";
byte xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("items.txt"));
System.out.print("Enter 1 for Food: ");
try{
xpenseCode = Byte.parseByte(kb.nextLine());
double sum = 0.0;
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
amount = Double.parseDouble(parts[3]);
expense = parts[1];
if(xpenseCode == 1){
if (expense.compareToIgnoreCase("tuition")==0) {
sum += amount;
}
}
}
System.out.printf("The total expense of Food is %.2f%n", sum);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error");
}
return total;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480764%2fhow-do-i-get-certain-data-within-a-text-file-using-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have figured it out.
public static double totalTuitionExpense() throws IOException{
double total =0;
double amount = 0;
String expense= "";
byte xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("items.txt"));
System.out.print("Enter 1 for Food: ");
try{
xpenseCode = Byte.parseByte(kb.nextLine());
double sum = 0.0;
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
amount = Double.parseDouble(parts[3]);
expense = parts[1];
if(xpenseCode == 1){
if (expense.compareToIgnoreCase("tuition")==0) {
sum += amount;
}
}
}
System.out.printf("The total expense of Food is %.2f%n", sum);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error");
}
return total;
}
add a comment |
I have figured it out.
public static double totalTuitionExpense() throws IOException{
double total =0;
double amount = 0;
String expense= "";
byte xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("items.txt"));
System.out.print("Enter 1 for Food: ");
try{
xpenseCode = Byte.parseByte(kb.nextLine());
double sum = 0.0;
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
amount = Double.parseDouble(parts[3]);
expense = parts[1];
if(xpenseCode == 1){
if (expense.compareToIgnoreCase("tuition")==0) {
sum += amount;
}
}
}
System.out.printf("The total expense of Food is %.2f%n", sum);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error");
}
return total;
}
add a comment |
I have figured it out.
public static double totalTuitionExpense() throws IOException{
double total =0;
double amount = 0;
String expense= "";
byte xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("items.txt"));
System.out.print("Enter 1 for Food: ");
try{
xpenseCode = Byte.parseByte(kb.nextLine());
double sum = 0.0;
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
amount = Double.parseDouble(parts[3]);
expense = parts[1];
if(xpenseCode == 1){
if (expense.compareToIgnoreCase("tuition")==0) {
sum += amount;
}
}
}
System.out.printf("The total expense of Food is %.2f%n", sum);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error");
}
return total;
}
I have figured it out.
public static double totalTuitionExpense() throws IOException{
double total =0;
double amount = 0;
String expense= "";
byte xpenseCode = 0;
Scanner kb = new Scanner(System.in);
Scanner read = new Scanner(new File("items.txt"));
System.out.print("Enter 1 for Food: ");
try{
xpenseCode = Byte.parseByte(kb.nextLine());
double sum = 0.0;
while (read.hasNextLine()){
String oneLine = read.nextLine();
String parts = oneLine.split(",");
amount = Double.parseDouble(parts[3]);
expense = parts[1];
if(xpenseCode == 1){
if (expense.compareToIgnoreCase("tuition")==0) {
sum += amount;
}
}
}
System.out.printf("The total expense of Food is %.2f%n", sum);}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error");
}
return total;
}
answered Nov 27 '18 at 11:29
Joey DeguzmanJoey Deguzman
406
406
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480764%2fhow-do-i-get-certain-data-within-a-text-file-using-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You test whether the item in the line matches the entered expense code, and if so, add the cost.
– daniu
Nov 26 '18 at 12:11
1
What exactly is the problem? All you have to do is to check if the second element of
parts
is the type you want (Food
in this case), and if it is then add the price to some local variable storing total food expenses.– Amongalen
Nov 26 '18 at 12:14
1
Why
Byte.parseByte()
?– Joakim Danielson
Nov 26 '18 at 14:08