-3

Is there a way to count the number of words in a string input at the start of a program and use that number as number of command line arguments in a public static void main (String[] args) program?

The assignment says:

Write a program with which you can consign any number of command line parameters (strings). The consigned parameters should then be output under specification of their position (their number)

  • Command line arguments are to be given while running the program. – Sanket Makani May 11 '17 at 09:12
  • 1
    That doesn't quite make sense..can you elaborate more on what exactly you want to achieve? – Akshay May 11 '17 at 09:13
  • 1
    Also, `in a public static void main (String[] args) program`. Every Java program is a `public static void main (String[] args)` program. – Ivar May 11 '17 at 09:15
  • I want the program to do the following: Read in a string and use each word as one argument. Then output the arguments that have been input before, but in the following fashion: Input: Hello world. Output: (1) Hello (2) World – Arkaine Ascorbin May 11 '17 at 09:21

2 Answers2

0

How to get get number of words ? Here is it

   Scanner sc = new Scanner(System.in);
   String []s = sc.nextLine().split(" ");
   System.out.println("Number of words : "+s.length);

the rest you can figure it out

Kangkan
  • 127
  • 1
  • 2
  • 10
  • This already helped me a lot. Now I only need to know how to access each argument and output it, so i can do the following: Input = "Hello World". Output = "Word count: 2, (1) Hello (2) World". So far I got this: for (int i = 0; i < sc.length; i++) { System.out.println ("("+???+")" + ???);} while ??? are the arguments I don't know which to put in – Arkaine Ascorbin May 11 '17 at 09:37
  • It's actually not a homework assignment. It's a java exercise which doesn't have to be done but I wanted to know how to do it anyway. – Arkaine Ascorbin May 11 '17 at 09:50
  • I don't care about downvotes or stuff. I just want to learn java and as long as I get the help I need I'm fine with it. It was a rather stupid question anyways. – Arkaine Ascorbin May 11 '17 at 09:54
0

I figured it out myself. What I wanted to do was the following:

import java.util.Scanner;

public class HelloJava {
    public static void main (String[] args) {
        System.out.println ("Hello JAVA");
        System.out.println ("Please enter your text: ");

        Scanner scan = new Scanner(System.in);
        String []s = scan.nextLine().split(" ");
        System.out.println("Number of words : "+s.length);
        scan.close();
        for (int i = 0; i < s.length; i++) {
            int counter = i+1;
            System.out.println ("("+counter+")" + s[i]);
        }           
    }
}

And it works now like a charm!

  • Then I'm not sure what the command line arguments have to do with it. – Ivar May 11 '17 at 09:53
  • A friend of mine did it by converting everything into char, scanning the line for spaces, using those as separators and a whole lot more unnecessarily complicated functions, but my professor told us, that it can be done a lot easier with the String args class. – Arkaine Ascorbin May 11 '17 at 10:02
  • Are you sure is that what you need. That code doesn't take the parametes. You can test this code by creating another class and calling the this class main method. (i.e `HelloJava.main(new String[]{"Hello", "JAVA"});`) – JCalcines May 11 '17 at 10:07
  • Your professor could mean that you start your program with command line parameters. In that case you only need the for loop with `args` instead of `s`. When you call a java programm with command line parameters, every word will be a separate parameter, so it will be split by default. – Ivar May 11 '17 at 10:10
  • It was not that difficult. I think you should have figured it by yourself – Kangkan May 11 '17 at 10:14
  • Well, the text of the exercise says (translated from german): "Write a program with which you can consign any number of command line parameters (strings). The consigned parameters should then be output under specification of their position (their number). – Arkaine Ascorbin May 11 '17 at 10:18
  • You should add that information to your question. That looks different from what you asked and the answer is simpler than this code. – JCalcines May 11 '17 at 10:30
  • Added it. And even simpler? How so? – Arkaine Ascorbin May 11 '17 at 10:37