Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,200,120 members, 7,973,778 topics. Date: Sunday, 13 October 2024 at 06:38 AM |
Nairaland Forum / Science/Technology / Programming / Need Help, Please Help! Java (1318 Views)
Please Help, Java Applet And Asp.net Page / Help Java Programmers: Need Help With A Java Assignment / HELP!! Java Programmers In The House Pls Help. (2) (3) (4)
Need Help, Please Help! Java by Nobody: 4:18pm On Aug 26, 2014 |
import java.util.Scanner; import java.io.*; public class mean { Scanner in = new Scanner (System.in); public static void main (String[] args) { try { Scanner in = new Scanner (System.in); double digit; double sum = 0; int count = 0; double average = 0; System.out.println("Please Enter number, Q to quit" Integer.parseInt(in.next()); while(in.hasNextDouble()) { digit = in.nextDouble(); sum+=digit; count++; average = sum/count; } { System.out.println("sum is " + sum); System.out.println("average is " + average); } } catch(NumberFormatException en) { System.out.println("Error, Box only allows Numbers" System.out.println(en); System.exit(0); } } } "If i input 10 20 30 40, " it reads 20 30 40, and give me the average of three instead of 4" I guess it is because of this line Integer.parseInt(in.next()); when i cancel it, it reads 10 20 30 40, and gives me the right average but it doesn't print out my exception. "catch(NumberFormatException en) { System.out.println("Error, Box only allows Numbers" System.out.println(en); System.exit(0);" I guess i am just very very confused, would appreciate any help i get due tomorrow morning actually |
Re: Need Help, Please Help! Java by 50calibre(m): 4:31pm On Aug 26, 2014 |
It's been a long time I touched java, I'm very rusty but if I may ask, what exactly do you want this programme to do, because if it's just to accept a series of numbers and print the average out, then your code is too complicated for such simple task |
Re: Need Help, Please Help! Java by Nobody: 4:42pm On Aug 26, 2014 |
50calibre: It's been a long time I touched java, I'm very rusty but if I may ask, what exactly do you want this programme to do, because if it's just to accept a series of numbers and print the average out, then your code is too complicated for such simple task if they input a string, i want it to print out an error statement saying this "System.out.println("Error, Box only allows Numbers", that is basically where i have a problem |
Re: Need Help, Please Help! Java by 50calibre(m): 4:54pm On Aug 26, 2014 |
Ok so I'm guessing it now prints out the numbers accordingly & calculates the average of all numbers? Is your problem only with the exception bit? Btw what IDE are you using, as in what platform are you using to run your program? |
Re: Need Help, Please Help! Java by Nobody: 6:26pm On Aug 26, 2014 |
50calibre:it doesnt do it accordingly, if have 10, 20, 30, 40, it counts 20, 30, 40 and leaves out 10 im using drJava yes, the exception is giving me problem |
Re: Need Help, Please Help! Java by javadoctor(m): 7:05pm On Aug 26, 2014 |
Make it code simple It's best the condition terminates in the while block so I wld hv done this; While(!in.hasNextLine()){ // get scanner // add numbers // initialize total }else { //code terminate // print average } But ofcourse this doesn't validate against Q, to do that . String num = in.nextLine(); While(num!="Q"{ // sum Integer.parseInt(num); // initialize total }else { //print average //terminate program } |
Re: Need Help, Please Help! Java by Nobody: 7:20pm On Aug 26, 2014 |
i am not on my program, but where you able to make the exception work this way? looks like you are really a java doctor |
Re: Need Help, Please Help! Java by 50calibre(m): 7:33pm On Aug 26, 2014 |
Donlittle: Cool! DrJava is very good, since the program produces results, that means it's compiles fine & won't highlight your error lines. therefore you'd have to figure out why it's omitting 10 First, this line you identified, may have something to do with it. Integer.parseInt(in.next()); Check your declarations, you declared digit as a double, and you're taking in an integer.. Integer.parseInt(in.next()); while(in.hasNextDouble()) Check the above lines, it may be the problem I'm not too sure As for the exception, why not try an IF statement, something like this if(input.hasNextInt){ // execute the following codes, } else { System.out.println (" Error!!! Please enter a number " } I no longer have the laptop I used in Uni, else I could have ran this program of yours, to see for myself, It's much easier to pin point the problem that way. Just keep fiddling those codes anyway, you'd get it right. |
Re: Need Help, Please Help! Java by Nobody: 7:42pm On Aug 26, 2014 |
javadoctor: Hey, dr java, i have tried this, and it brings out this error line Syntax error on token "else", delete this token |
Re: Need Help, Please Help! Java by Nobody: 10:20pm On Aug 26, 2014 |
Thanks Everyone, i got it working(With a little help from my asian friend) different ways to work around it though, i will try everyone else idea till i 100% get everything import java.util.Scanner; import java.io.*; public class mean3 { Scanner in = new Scanner (System.in); public static void main (String[] args) { try { Scanner in = new Scanner (System.in); double digit = 0; double sum = 0; int count = 0; double average = 0; System.out.println("Please Enter number, Q to quit" while(in.hasNextDouble()) { digit = in.nextDouble(); sum+=digit; count++; average = sum/count; } if(in.hasNextDouble() == false) { String s = in.next(); if (s.equals("Q") { System.out.println("Sum is " + sum); System.out.println("Average is " + average); } else { Integer.parseInt(s); } } } catch(NumberFormatException en) { System.out.println("Error, Box only allows Numbers" System.out.println(en); System.exit(0); } } } |
Re: Need Help, Please Help! Java by asalimpo(m): 3:32pm On Aug 27, 2014 |
Alternatively, capture all d numbers in one string. - tokenize the string ,at the spaces , into a collection (or array) - loop thru d array,parsing each element to an integer. I.e convert the collection of strings to a collection of numbers - perform d computation. Sum-of-numbers/ size of numbers collection. - catch and handle errors/exceptions. More convenient thn prompting user for every next input. No scanner wahala. |
Re: Need Help, Please Help! Java by Nobody: 4:43pm On Sep 02, 2014 |
. |
Re: Need Help, Please Help! Java by Nobody: 4:45pm On Sep 02, 2014 |
import java.util.Scanner; import java.io.*; import javax.swing.*; import java.util.Arrays; public class liveConversion extends ForDollar { public static void main(String[] args) { Scanner in = new Scanner(System.in); Double amount; //String Nigeria, America, England, Japan, Euro; //amount = in.nextDouble(); String nigeria; String america; String england; String japan; String euro; ForDollar dollar = new ForDollar(); String input1, input2, input3; } } |
Re: Need Help, Please Help! Java by Nobody: 4:46pm On Sep 02, 2014 |
import java.util.Scanner; import javax.swing.*; public class ForDollar { Scanner in = new Scanner(System.in); private double amount; String input2; String input3; String input1 = JOptionPane.showInputDialog(null, "What Country Money Do you have" { if(input1.equals("Dollars") { input1 = input2; input2 = JOptionPane.showInputDialog(null, "input the currency you are converting to." } if(input2.equals("Pounds") { amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much pounds do you have"); double d = 2; double newAmount = amount/d; JOptionPane.showMessageDialog(null, "You now have ##" + newAmount); System.exit(0); } else if (input2.equals("Naira"); { amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much Naira do you have"); double d = 2; double newAmount = amount * d; JOptionPane.showMessageDialog(null, "You now have ##" + newAmount); System.exit(1); } else if (input2.equals("Japan"); { double d = 3; amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much Naira do you have"); double newAmount = amount/d; JOptionPane.showMessageDialog(null, "You now have yen" + newAmount); } } } So i am back again and this time, my else if statement gives me an error, it says 'else' without 'if' angry angry i know it is something little, but i have tried everything, damn it, school just started and my professor is killing me, It works well, but when i add my second else if statement, it just wont do anything, it says 'else' without 'if' if i take that out, it compiles, but i still have to add so many else if statement, so i am currently confused |
Re: Need Help, Please Help! Java by QuickSuccess: 9:01pm On Sep 16, 2014 |
why do you have the ; sign after ur if statements? that causes your if statement to be seen as empty. remove them. plus, ur code seems wrong, the whole conversion stuff is confusing, not my issue though, i wrote ur code again for u: import java.util.Scanner; import javax.swing.*; public class ForDollar { public static void main(String[] args) { Scanner in = new Scanner(System.in); double amount; String input2 = null; String input1 = JOptionPane.showInputDialog(null, "What Country Money Do you have" if(input1.equals("Dollars") { input1 = input2; input2 = JOptionPane.showInputDialog(null, "input the currency you are converting to." } if(input2.equals("Pounds") { amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much pounds do you have"); double d = 2; double newAmount = amount/d; JOptionPane.showMessageDialog(null, "You now have ##" + newAmount); System.exit(0); } else if (input2.equals("Naira") { amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much Naira do you have"); double d = 2; double newAmount = amount * d; JOptionPane.showMessageDialog(null, "You now have ##" + newAmount); System.exit(0); } else if (input2.equals("Japan") { double d = 3; amount = Double.parseDouble(JOptionPane.showInputDialog(null, "How much Naira do you have"); double newAmount = amount/d; JOptionPane.showMessageDialog(null, "You now have yen" + newAmount); } } } |
Re: Need Help, Please Help! Java by Nobody: 10:34pm On Sep 16, 2014 |
Thanks, I already figured out everything and rewrote the code from scratch, and yes, I found out the ; weren't needed, thanks still though |
(1) (Reply)
Which Is The Best Bulk SMS Service Provider? / HACKWARE101: Getting Started With The Arduino Uno / Young Wordpress Developer Living In Ikeja
(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. 40 |