473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem with a program that is suppose to display characters per line

Crazy as this sounds this program is actually working! I know I know it is amazing I got one to run. The only problem I am having is I want it to say for output line (1,2 3 etc.) has a length (25,30,35 etc) characters, and then at the end state combined sum totals. But instead it is reading the entire line instead of giving each line a number! For example, it will say Line How Are You! has a length 12 characters. How do I lose the How Are You! and replace it with its perspective line #?? Thanks so much in advance. ALI :P

// Lists contents of a user-specified file


import java.util.*;

import java.io.*;



public class W6Ex64 {



// main(): application entry point

public static void main(String [] args) throws IOException {


// set up standard input stream

Scanner stdin = new Scanner(System.in);



// determine file

System.out.print("Filename: ");

String name = stdin.nextLine();

File file = new File(name);



// set up file stream

Scanner fileIn = new Scanner(file);



// initially no values have been processed

int valueSum = 0;

// process lines one by one

while (fileIn.hasNext()) {

// get next line

String line = fileIn.nextLine();



// process current line
int newline = line.length();
valueSum += newline;

//Display Results

System.out.println("Line " + line + " has a length " + newline + " characters.");
}

// compute the sum of all lines
if (valueSum >0) {
int sum = ++valueSum;
System.out.println("Toal Characters in this file: " + sum);
}



}
}
Apr 27 '07 #1
2 1599
I didn't understand what do you want ..
Apr 27 '07 #2
I need to increment a line counter inside my while statement, and I dont know how to do that! That will allow the line number rather then the quote of the line to show up in this particular program.


Crazy as this sounds this program is actually working! I know I know it is amazing I got one to run. The only problem I am having is I want it to say for output line (1,2 3 etc.) has a length (25,30,35 etc) characters, and then at the end state combined sum totals. But instead it is reading the entire line instead of giving each line a number! For example, it will say Line How Are You! has a length 12 characters. How do I lose the How Are You! and replace it with its perspective line #?? Thanks so much in advance. ALI :P

// Lists contents of a user-specified file


import java.util.*;

import java.io.*;



public class W6Ex64 {



// main(): application entry point

public static void main(String [] args) throws IOException {


// set up standard input stream

Scanner stdin = new Scanner(System.in);



// determine file

System.out.print("Filename: ");

String name = stdin.nextLine();

File file = new File(name);



// set up file stream

Scanner fileIn = new Scanner(file);



// initially no values have been processed

int valueSum = 0;

// process lines one by one

while (fileIn.hasNext()) {

// get next line

String line = fileIn.nextLine();



// process current line
int newline = line.length();
valueSum += newline;

//Display Results

System.out.println("Line " + line + " has a length " + newline + " characters.");
}

// compute the sum of all lines
if (valueSum >0) {
int sum = ++valueSum;
System.out.println("Toal Characters in this file: " + sum);
}



}
}
Apr 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Stephen Harris | last post by:
I downloaded: Windows Binaries PHP 5.0.4 zip package - 31 Mar 2005 md5: fff10991a8e6f3b36ee567eb236ae3f4 This is the current BETA release of version 5.0 of the MySQL database server....
3
by: Salgoud Dell | last post by:
I have a VB6 application running on a French computer using Windows ME. The app doesn't work the same way as it does when running on an English Windows OS. The main issue I have, that I can't...
4
by: Tony C | last post by:
I've written a program which reads a text file in , and writes it out to a new filename. When the program encounters a line which contains a specific text label-, I read in the hex number...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
12
by: jois.de.vivre | last post by:
Hi, I have the following piece of code that is designed to help me add debug traces to my program (I wanted to use purely C++ code, but the only way I know how to do something like this is with...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
8
by: morphex | last post by:
Hi, I'm a python programmer that's started to play a bit with C as I'll probably have to make C extensions eventually.. I made this little program that I'd like to get feedback on, it's...
12
by: manstey | last post by:
I am writing a program to translate a list of ascii letters into a different language that requires unicode encoding. This is what I have done so far: 1. I have # -*- coding: UTF-8 -*- as my...
11
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.