The Artima Developer Community
Sponsored Link

Java Answers Forum
I need help on simple java.

7 replies on 1 page. Most recent reply: Mar 31, 2011 2:13 AM by dasari vaass

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Mahlet Garedov

Posts: 3
Nickname: dabest
Registered: Feb, 2011

I need help on simple java. Posted: Feb 19, 2011 7:12 AM
Reply to this message Reply
Advertisement
I need to write a Java class called ArgStats.
This should process all the command line arguments and do the following.
1.If there are no arguments, or one or more are not integers, it should print the message: “Please supply one or more integers; only integers are allowed”

2.If all arguments are integers, it should print the number of arguments, the total, and the average.

Note: the command line arguments are passed to the main method as args, given the method signature public static void main (String[] args).
I tried to do this, but I need help in some parts of 1st statement.And I dont know how to calculate and show number of arguments in 2nd statement.

My java code is this:


import java.util.Scanner;

public class ArgStats {

public static void main(String[] args) {
Scanner S = new Scanner(System.in);

int tot = 0;
int avrg = 0;
while (S.hasNextDouble()) {
double x = S.nextDouble();
tot +=x;
avrg++;
System.out.println("read: " + x);
}

int length = args.length;
if (length <= 0) {

System.out.println("Please supply one or more integers; only integers are allowed");

} else {

System.out.println("Total = " +(double) tot);
System.out.println("Average = " + (double) tot/avrg);

}
}
}


Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: I need help on simple java. Posted: Feb 19, 2011 7:56 AM
Reply to this message Reply
This will work:


public class ArgStats {
static int total=0;
static double average;
static int i;

public static void main(String[] args) {

if (args.length == 0) {
System.out.println("Please supply one or more integers; only integers are allowed");

}
else{
for (i = 0; i < args.length; i++) {
try {
total += Integer.parseInt(args[ i ].trim());
} //TAKE OUT THE SPACES IN [ I ] I had to write it this way for the forum
catch (NumberFormatException nfe){
System.out.println("Please supply one or more integers; only integers are allowed");
System.exit(1);
}
}
average = ((double)total/i);
System.out.println("Number of Arguments = " + i + "\nTotal = " + total + "\nAverage = " + average);
}
}
}

Do you know how to compile a program from the command line with arguments? To run this program type "java ArgStats 5 12 4520 356" (or whatever numbers you want) in the command line.
No need for scanner unless you want to take the arguments one at a time.

Mahlet Garedov

Posts: 3
Nickname: dabest
Registered: Feb, 2011

Re: I need help on simple java. Posted: Feb 19, 2011 8:24 AM
Reply to this message Reply
Thank you very much for your help, but in IntelliJ when I run your code it direcly prints

"Please supply one or more integers; only integers are allowed
Process finished with exit code 0" in command line.

how can I test it in command line?

Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: I need help on simple java. Posted: Feb 19, 2011 9:46 AM
Reply to this message Reply
You need to run it from the command line, not an IDE.
open cmd.exe on your computer. Move to the directory where you saved ArgStats.java using the commands cd (changedirectory) and dir (directory) to see whats in the current level. type: "javac ArgStats.java" to compile. Then run it with "java ArgStats <args>". For example: "java ArgStats 10 50 248 51"

http://www.youtube.com/watch?v=5u8rFbpdvds&feature=relmfu

good luck!

Mahlet Garedov

Posts: 3
Nickname: dabest
Registered: Feb, 2011

Re: I need help on simple java. Posted: Feb 19, 2011 10:45 AM
Reply to this message Reply
I appreciate your help,thank you very much.

But does it mean that my codes were wrong? because I need to run and test in in Java IntelliJ.

and when I try the way you say I get error in command line,I do exactly as youtube video, but when I write "javac ArgStats.java" to compile it, I get this error:
"javac" is not recognized as an internal or external command,operable program or batch file.

Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: I need help on simple java. Posted: Feb 21, 2011 2:04 PM
Reply to this message Reply
That means you don't have java installed. Watch his first video. But I dont know how to do it in IntelliJ

Anil Reddy

Posts: 2
Nickname: anilreddyv
Registered: Mar, 2011

Re: I need help on simple java. Posted: Mar 16, 2011 2:51 AM
Reply to this message Reply
Try to pass the arguments while running the program like as follows

right click on the program and u can see Run As--> Runconfigurations click on this then it will open a window in that window there is a tab Arguments click on that it will open a rich text box there provide the command line arguments and press apply button and click on run then the program will run as u expected.

dasari vaass

Posts: 1
Nickname: vaassdasar
Registered: Mar, 2011

Re: I need help on simple java. Posted: Mar 31, 2011 2:13 AM
Reply to this message Reply
Hi,

any how u got the clear output from below, this is another simple way.......

hope u like this..



import java.util.Scanner;

public class cmdsample{

static int total=0;
static double avg;

public static void main(String[]a)
{

try
{

for(String s: a) // Enchanced For Loop Java 5.0

{

total+=Integer.parseInt(s);
}



avg=((double)total/a.length);

System.out.println("Average"+avg);

System.out.println("Number of Integers.."+a.length);

System.out.println("Total ."+total);

}
catch(NumberFormatException ne)
{

System.out.println("Please Allow only integers.....");

}

}

}

Flat View: This topic has 7 replies on 1 page
Topic: Sequence Push Consumer Previous Topic   Next Topic Topic: ArrayList -

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use