Solved

new work with java

Posted on 2009-05-08
13
256 Views
Last Modified: 2013-11-23
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++;
}
0
Comment
Question by:lpetrowicz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 7

Expert Comment

by:zwei
ID: 24341213
Print out to command prompt is done System.out.println() command.

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

Open in new window

0
 
LVL 7

Expert Comment

by:zwei
ID: 24341216
Oops forgot a quotation mark
System.out.println("Total of " + countFours + " fours.");

Open in new window

0
 

Author Comment

by:lpetrowicz
ID: 24341223
so, everything else is in the correct spot then and written correctly?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:lpetrowicz
ID: 24341234
and if so, exactly where in my code am I putting it
0
 
LVL 7

Expert Comment

by:zwei
ID: 24341260
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

0
 
LVL 7

Expert Comment

by:zwei
ID: 24341273
Sorry for the messed up indentation. Dunno how I did that... o.O
0
 

Author Comment

by:lpetrowicz
ID: 24341289
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
0
 

Author Comment

by:lpetrowicz
ID: 24341293
to include whitespace
0
 
LVL 7

Expert Comment

by:zwei
ID: 24341331
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

0
 
LVL 7

Expert Comment

by:zwei
ID: 24341349
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

0
 
LVL 7

Expert Comment

by:zwei
ID: 24341353
Ahh >.<
totalChars += text.length() + 1;

Open in new window

0
 

Author Comment

by:lpetrowicz
ID: 24341371
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?
0
 
LVL 7

Accepted Solution

by:
zwei earned 500 total points
ID: 24341408
Maybe this is less confusing: Counts the fours and counts all the characters, and does two printouts to command prompt:
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("C:\\test.txt");
        BufferedReader reader = null;
        int countFours = 0;
        int totalChars = 0;
 
 
        try {
            reader = new BufferedReader(new FileReader(file));
            int c = 0;
 
            // read the characters, one at a time.
            while ((c = reader.read()) != -1) {
                totalChars++;
                    if (c == '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

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
Crystal Reports Licensing Questions 4 65
wild fly 8 startup error 2 43
Given a github repo find email address of all collaborators in that repo 1 36
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question