Link to home
Start Free TrialLog in
Avatar of lpetrowicz
lpetrowicz

asked on

new work with java

Below I have listed some code I am working on.  I am trying to get to count how many fours I have in my text on each line.  The lines in the text will end with a comma before I start the next line.

I believe I have the input of the text correct and most of the counting portion.  I am lost on the rest of  it and the output.  Can you show me a way t put the output in a text and/or return in in the prompt area

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class MyTry1 {
    public static void main(String[] args) throws IOException {
        try {
            inputStream =
                new BufferedReader(new FileReader("readme.txt"));
String lineInfo = input.nextLine();
       
// get the number of characters in each line    
       
counts+= lineInfo.length();

// get the number of 4's in each line    
   
char[] chars = lineInfo.toCharArray();
for (int i = 0; i < chars.length; i++)
{
  if (chars[i] == '4')
     countFours++;
}
Avatar of zwei
zwei
Flag of Sweden image

Print out to command prompt is done System.out.println() command.

Example:
System.out.println("Total of " + countFours + " fours);

Open in new window

Oops forgot a quotation mark
System.out.println("Total of " + countFours + " fours.");

Open in new window

Avatar of lpetrowicz
lpetrowicz

ASKER

so, everything else is in the correct spot then and written correctly?
and if so, exactly where in my code am I putting it
Well no, you're only reading the first line of the text file right now. To read all lines you'd have to loop them.

I would do it like this:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class MyTry1
{
    public static void main(String[] args)
    {
        File file = new File("example.txt");
        BufferedReader reader = null;
        int countFours = 0;
 
 
            try {
				reader = new BufferedReader(new FileReader(file));
            String text = null;
 
            // repeat until all lines is read
				while ((text = reader.readLine()) != null)
				{
					for(int i = 0; i < text.length(); i++) {
						if (text.charAt(i) == '4') {
							countFours++;
						}
					}
				}
				System.out.println("Total fours: " + countFours);
			} catch (IOException e) {
				e.printStackTrace();
			}
    }
}

Open in new window

Sorry for the messed up indentation. Dunno how I did that... o.O
One last question

I need to add an object that tells me how many characters per line, can you help with that or do I need a whole new question.  This other part works awesome...Thanks
to include whitespace
in my example, text.length() holds the number of characters for the line. If you wanted the total lines in the text file you could just make another int variable:
public class MyTry1 {
    public static void main(String[] args) {
        File file = new File("C:\\test.txt");
        BufferedReader reader = null;
        int countFours = 0;
        int totalChars = 0;
 
 
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
 
            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                totalChars += text.length();
                for(int i = 0; i < text.length(); i++) {
                    if (text.charAt(i) == '4') {
                        countFours++;
                    }
                }
            }
            System.out.println("Total fours: " + countFours);
            System.out.println("Total characters in the file: " + totalChars);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

Oh yeah, reader.readLine() does not include the new line feed. So you would have add that if you wanted the new line white space to count too:
totalChars += += text.length() + 1;

Open in new window

Ahh >.<
totalChars += text.length() + 1;

Open in new window

A litle confused now

The second bit of code you wrote and added; that would count characters per line and tell me total number of 4's?
ASKER CERTIFIED SOLUTION
Avatar of zwei
zwei
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial