1

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.

4
  • Do you have a static main method? Read about that, and you'll see how to pass arguments to your application. Commented Jul 12, 2012 at 0:56
  • What is in? That may help us help you. Commented Jul 12, 2012 at 0:59
  • here is rest of the code, it didnt let me post it... Commented Jul 12, 2012 at 1:05
  • 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; Commented Jul 12, 2012 at 1:05

4 Answers 4

3

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];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

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;
Post your code in your original posting so we can read it. It needs to compile and run, of course.
0

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/

Comments

0

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());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.