Shawn
Shawn

Reputation: 307

How to parse a String in Java?

This is my code:

   import java.io.*;
   import java.util.*;

   public class Testing {
       public static void main(String[] args) {
           Scanner in = new Scanner(System.in );
           PrintStream out = System.out;

           String input = in .next();
           String output = "";
           char c = 0;
           int count = 0;
           int countInput = input.length();

           for (int i = 0; i < countInput; i++) {
               if (i == 0) {
                   c = input.charAt(i);
                   count++;
               } else {
                   if (c == input.charAt(i)) {
                       count++;
                   } else {
                       output = output + count + "" + c;
                       count = 1;
                       c = input.charAt(i);
                       out.println(output);
                   }
               }
           }
           output = output + count + "" + c;
           out.println(output);
       }
   }

This program is suppose to work like this:

Input:

java Testing AAAAAnnnfffkk

Output:

5A3n3f2k

I need to fix this somehow:

String input = in.next();

I think args has to be used somewhere, I'm not sure where though.

Upvotes: 1

Views: 1608

Answers (4)

Margus
Margus

Reputation: 20038

In code example:

//importing lib
import java.util.Scanner;

//file name "Hello.java"
public class Hello {
    //Main method
    public static void main(String[] args) {
        //input reader
        Scanner input = new Scanner(System.in);
        //output storage
        StringBuilder output = new StringBuilder("");


        //read a line, you can use other command in this class to parse
        output.append(input.nextLine());
        //write a line
        System.out.println(output.toString());
    }
}

Edit:

If you like to use args then :

public class Hello {
    // Main method
    public static void main(String[] args) {
        // output storage
        StringBuilder output = new StringBuilder("");
        // check if there are arguments
        if (args.length > 0) {
            // use string
            output.append(args[0]);
        }
        // write a line
        System.out.println(output.toString());
    }
}

Upvotes: 0

Ruel
Ruel

Reputation: 15780

Your program logic is ok. But based on what you want to do, you won't be needing the Scanner class.

If you want your input to come from the arguments array : main(String[] args), then you need to assign that to your input:

String input = args[0];

But hey, what if you ran the program without giving arguments? It will throw an exception, so you need to have some sort of error handling:

String input = "";
if (args.length > 0) {
    input = args[0];
}

Upvotes: 3

triple_t91
triple_t91

Reputation: 21

You definitely need a main method. Try reading through this link about the main. Hope it helps. http://www.dreamincode.net/forums/topic/38921-a-closer-look-at-the-main-method/

Upvotes: 0

Tony Ennis
Tony Ennis

Reputation: 12289

We can go through this a step at a time.

First, write a program to get the input, and then print it one character at a time.

The reason is that you're going to need to read the input, and process it one character at a time, so the first step is to get the loop mechanics down.

Once you're looping correctly, we can worry about the logic that counts repeated letters.

Edit your post when that's done and I'll post more here.

Upvotes: 1

Related Questions