Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,688 members, 7,955,580 topics. Date: Sunday, 22 September 2024 at 09:40 AM |
Nairaland Forum / Science/Technology / Programming / Please Help To Solve This Java Problem. (1194 Views)
Please Solve This Java Problem. / Pls, Help Me In This Java Problem / Simple Java Problem (2) (3) (4)
Please Help To Solve This Java Problem. by skoten(m): 12:38pm On Jul 04, 2010 |
I need someone to do this using java or C A man just purchased a cd pack containing recharge vouchers. Each recharge voucher has 8 digits. Your task is to write a program to input a list of all the vouchers and output the sum of all digits in the vouchers. Sample Input 00000001 10000002 20000003 Sample output 9 |
Re: Please Help To Solve This Java Problem. by Beaf: 9:50pm On Jul 04, 2010 |
Can't you just create an array and loop it? |
Re: Please Help To Solve This Java Problem. by skoten(m): 9:55pm On Jul 04, 2010 |
@ Beaf I already av a solution but pls let me c urs, thanks |
Re: Please Help To Solve This Java Problem. by Beaf: 9:59pm On Jul 04, 2010 |
skoten: Then, you should have posted your solution and asked if anyone could improve or provide something better. Post your solution. |
Re: Please Help To Solve This Java Problem. by xtraodinaire: 10:46pm On Jul 04, 2010 |
lmao!!! |
Re: Please Help To Solve This Java Problem. by Bossman(m): 2:25pm On Jul 05, 2010 |
OP, why don't you post what you have. This sounds like a homework problem to me. You are already on the right track by solving this yourself, because in the "real world" you will need to solve problems yourself. |
Re: Please Help To Solve This Java Problem. by JFrame: 3:22pm On Jul 07, 2010 |
Why do you need alternative when you have the solution. forums are meant to help with problems. |
Re: Please Help To Solve This Java Problem. by skoten(m): 2:28pm On Jul 08, 2010 |
import java.util.*; import javax.swing.*; class Recharge { public static void main(String args[]) { int a = Integer.parseInt(JOptionPane.showInputDialog("Enter number of Vouchers: "); int TotalSum = 0; int sum[] = new int[a]; int div[] = new int[a]; int Voucher[] = new int[a]; for(int i=0; i<=Voucher.length; i++){ sum[i] = 0; Voucher[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter Digits for the Voucher[]: "); while (Voucher[i] > 0) { div[i] = Voucher[i] % 10; sum[i] = sum[i] + div[i]; Voucher[i] = Voucher[i] / 10; } System.out.println("\nSum of digits in Voucher is " + sum[i]); TotalSum = TotalSum + sum[i]; System.out.println("\nSum of all digits in the pack are " + TotalSum); } } } safe, |
Re: Please Help To Solve This Java Problem. by gozzilla(m): 5:57pm On Jul 08, 2010 |
First you have a nice code there. It actually solves the problem in hand. Right. But if you need to do this for say 100 or even a 1000 voucher how long will it take you to do this. So assuming this is a code for a "commercial " or even a program to help, i think a Japanese calculator will do fine. So here is your solution. The voucher number can easily be save in a file. Your program will read the numbers from the file. get a file read the file get each voucher(item) do operation; Finally, no one seems to want to touch your code cos you ainnt so clear about what you want done. |
Re: Please Help To Solve This Java Problem. by greatgeo(m): 1:13pm On Jul 09, 2010 |
am currently having some problems with installation of java cd any help |
Re: Please Help To Solve This Java Problem. by Beaf: 6:44pm On Jul 09, 2010 |
@skoten An idea would be to pass blocks (or all, depending on size) of your input data as a pre concatinated string, convert into a char array, iterate, convert to in and sum. You could polish what I given below; Sample Input 00000001 10000002 20000003 Becomes "000000011000000220000003" You can convert the code below into a method. //===================================Code follows int TotalSum = 0; String str = "000000011000000220000003"; char[] charArray = str.toCharArray(); for (char c : charArray) { TotalSum = TotalSum + Integer.parseInt(c); } |
Re: Please Help To Solve This Java Problem. by candylips(m): 8:29pm On Jul 09, 2010 |
I have added simple error handling to beef's code U need to have commons-lang.jar in your classpath //===================================Code follows int TotalSum = 0; String str = "000000011000000220000003"; char[] charArray = str.toCharArray(); for (char c : charArray) { if(CharUtils.isAsciiNumeric(c)){ TotalSum = TotalSum + CharUtils.toIntValue(c); } //TotalSum = TotalSum + Integer.parseInt(c); } |
(1) (Reply)
Javascript And HTML / Life And Times Of Tech Billionaire Jason Li (no. 2) / Where Can One Find An Investor For Startup Businesses
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 62 |