The Artima Developer Community
Sponsored Link

Java Answers Forum
Counter Newbie Help

6 replies on 1 page. Most recent reply: Aug 13, 2003 3:39 PM by Joe Parks

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 6 replies on 1 page
Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Counter Newbie Help Posted: Mar 25, 2002 10:50 AM
Reply to this message Reply
Advertisement
I have to write a program that counts the number of words, sentences, and the number of occurrences of each alphebetic letter.

I already set up a GUI to bring in a .txt file but I am not sure where to start with the counters.

Any help at all would be good. Thanks.


Mohit Gupta

Posts: 15
Nickname: mohit
Registered: Mar, 2002

Re: Counter Newbie Help Posted: Mar 26, 2002 4:00 PM
Reply to this message Reply
just start counting after reading in file, and then use in.readline or readchar etc. method to read next-next line or char.. and count these in a while loop untill found end.
follow any beginners book, will def. help u..
like complete reference etc.

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Counter Newbie Help Posted: Mar 31, 2002 9:17 AM
Reply to this message Reply
thanks for your help :)

Ron

Posts: 2
Nickname: ryonn
Registered: Aug, 2003

Re: Counter Newbie Help Posted: Aug 12, 2003 8:05 PM
Reply to this message Reply
I want to write a program such that:

> java readinp
123 // Keyboard input

1 // Each character is printed 'vertically'
2
3

>

And here is my code:

----------------------------

import java.io.*;
import java.lang.String;


public class readinp
{

static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
String name;
char outp;
System.out.print("Enter something here: ");
name = input.readLine();
while(name.readChar() != '\n')
{
outp = name.readChar();
System.out.println(outp);
}

}
}
----------------------------

// However, it generates this message (after compiled):

readinp.java:15: cannot resolve symbol
symbol : method readChar ()
location: class java.lang.String
while(name.readChar() != '\n')
^
readinp.java:17: cannot resolve symbol
symbol : method readChar ()
location: class java.lang.String
outp = name.readChar();
^
2 errors

-------------------------------

Could someone please help me? Thanks

Ron

Posts: 2
Nickname: ryonn
Registered: Aug, 2003

Re: Counter Newbie Help Posted: Aug 12, 2003 8:07 PM
Reply to this message Reply
There is a typo above:

-----------

// I want to write a program such that:
> javac readinp.java
> java readinp
Enter something here: 123

1
2
3

>

-----------

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Counter Newbie Help Posted: Aug 13, 2003 2:57 AM
Reply to this message Reply
It's exactly what the error message says it is:

You're trying to call a method on String that doesn't exist.
If you consult the JavaDoc you should find a .charAt(int i) method.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Counter Newbie Help Posted: Aug 13, 2003 3:39 PM
Reply to this message Reply
Be careful of an infinite loop. The data read in via readLine() will never include the newline character.

This will do what you want:

package com.parksindustries.learning.linereader;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
/**
 * This simple application will show that the readLine()
 * method of a Reader (in this case, a BufferedReader) chomps
 * off the newline character(s).
 *
 * @author Joe Parks
 * @version 1
 * Date: Aug 13, 2003
 * Time: 5:23:32 PM
 */
public class Application {
 
    /**
     * Read from standard in using the readLine() method.
     * Iterate over the characters of the resultant string.
     * Note that there is no blank line between the last character
     * and the border ("<<<<<<>>>>>>").
     * @param args
     */
    public static void main(String[] args) {
        InputStreamReader inputStreamReader;
        inputStreamReader = new InputStreamReader(System.in);
        BufferedReader input;
        input = new BufferedReader(inputStreamReader);
        try {
            String line = input.readLine();
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                System.out.println("c = " + c);
            }
            System.out.println("<<<<<<>>>>>>");
        } catch (IOException e) {
            System.err.println("LOOK OUT!");
            e.printStackTrace();
        }
    }
}

Flat View: This topic has 6 replies on 1 page
Topic: null pointer exception error Previous Topic   Next Topic Topic: Help needed urgently

Sponsored Links



Google
  Web Artima.com   

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