Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
import java.io.*;
import java.util.*;
public class Practices {
public static void main(String[] args) throws FileNotFoundException
{
This is my text file:
To be or not to be that is
the question
what about now
so be it
Scanner input =new Scanner(new File("C:/Users/Charlie/workspace/Summerexercises/src/hamlet.txt"));
int countwords=0;
int countlines=0;
int countchar=0;
while(input.hasNext())
{
String word = input.next();
countwords++;
}
System.out.println("total words= "+ countwords);
this is the while loop, which count lines, but doesn't work propertly
while(input.hasNextLine())
{
String lines= input.nextLine();
countlines++;
}
System.out.println("total lines= "+ countlines);
input.close();
}
}
**This Loop will count the number of lines **
int count = 0;
while (scanner.hasNextLine()) {
count++;
scanner.nextLine();
}
Alternatively you can use LineNumberReader
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(new File("C:/Users/Charlie/workspace/Summerexercises/src/hamlet.txt")));
lineNumberReader.skip(Long.MAX_VALUE);
System.out.println(lnr.getLineNumber());
lineNumberReader.close();
Source : http://www.technicalkeeda.com/java/how-to-count-total-number-of-lines-of-file-using-java
You have to start from the beginning of the file again. By the time the first loop ends, you will be pointing to the EOF, so no lines are counted in the second loop. try commenting out the first loop and run this program.
In your first loop you consume all your Scanner content, the second loop gets never executed. Do this:
Scanner input =new Scanner(new File("C:/Users/Charlie/workspace/Summerexercises/src/hamlet.txt"));
int countwords=0;
int countlines=0;
int countchar=0;
while(input.hasNext())
{
String word = input.next();
countwords++;
}
System.out.println("total words= "+ countwords);
input.close();
input =new Scanner(new File("C:/Users/Charlie/workspace/Summerexercises/src/hamlet.txt"));
while(input.hasNextLine())
{
String lines= input.nextLine();
countlines++;
}
System.out.println("total lines= "+ countlines);
input.close();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
What I'm trying to do is validate a scanner so that if the input string doesn't have a minimum of two vowels it returns an error, however I'm not sure how to go about this and can't seem to find an answer elsewhere.
Scanner sc = new Scanner(System.in);
System.out.print("Input alphabetical character string: ");
while (!sc.hasNext("[aeiouAEIOU]+")) {
System.out.println("Error, sequence requires a minimum of two vowels");
sc.next();
}
you could use a validate method each time they enter a value something like this.this one only checks for each vowel once though.
public static void main(String[] args) {
// TODO Auto-generated method stub
String sInput = "";
Scanner sc = new Scanner(System.in);
System.out.print("Input alphabetical character string: ");
sInput = sc.next();
if (!hasTwoVowels(sInput)) {
System.out.println("Error, sequence requires a minimum of two vowels");
sc.next();
}
sc.close();
}
public static boolean hasTwoVowels(String sInput){
int iCount = 0;
String vowel[] = new String[]{"a","e","i","o","u","A","E","I","O","U"};
for(int i =0; i < 10;i++) {
if(sInput.contains(vowel[i])) {
iCount++;
if(iCount == 2) {
return true;
}
}
}
return false;
}
}
you could also split the string and count each letter then add up the vowels
you can easily do this by doing something like this.
arr[(int)'e' - (int)'a']++ that will increment arr[4] counting 1 e
you could also use a bufferedReader to count the vowels
This is a problem where using a Scanner makes the problem harder.
The simple solution is to just read the stream one character at a time using a BufferedReader and count the characters that are vowels.
You could do it by configuring a Scanner to return "tokens" consisting of a single character and then counting the tokens that match a vowel. But that's pointless ... IMO.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
need to combine two text files of girl names and boy names into one text file. the new file has to have the boy names and girl names separated into two lists of each gender, and we dont know how many names each file will have. the program runs but gets stuck in an infinite loop
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class NameTester
{
public static void main(String args[]) throws FileNotFoundException
{
Scanner userIn = new Scanner(System.in);
System.out.println("Enter file name for boys name file: ");
String boyFile = userIn.next();
System.out.println("Enter file name for girls name file: ");
String girlFile = userIn.next();
userIn.close();
Scanner boyIn = new Scanner(new FileReader(boyFile));
Scanner girlIn = new Scanner(new FileReader(girlFile));
PrintWriter out = new PrintWriter("names.txt");
out.print("Boys Names: Girls Names: ");
int count = 1;
while(boyIn.hasNextLine() || girlIn.hasNextLine());
{
String b = boyIn.next();
String g = girlIn.next();
out.print(count + " " + b + " " + count + " " + g);
count++;
}
boyIn.close();
girlIn.close();
out.close();
}
}
This line is an empty while loop that will run forever:
while(boyIn.hasNextLine() || girlIn.hasNextLine());
Get rid of the semicolon at the end:
while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
....
}
I haven't checked for other logic errors in your program, but this should get rid of the infinite loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The purpose of my code is to enter a pin, and it'll check if it's right or not. If it isn't, the question will loop.
For some reason, my code doesn't loop properly, and a lot of the code is underlined. Specifically the while loop itself and the second JOptionPane
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
while(correctPin != sMaybePin); <--
Look at that ; that terminates the loop right there. You need to remove that.
You never update sMabyPin which is the variable you are checking against. If you do what #John and #ANS suggested you'll be stuck in an infinite loop.
Remove the ; after the while statement and correct set the value of the variable sMaybePin to the input vlaue and ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to be able to read a desired character from the keyboard and locate its index. Any assistance would be appreciated!
import java.util.Scanner;
public class Q12Test
{public static void main(String args[])
{int index;
Scanner kb = new Scanner(System.in);
System.out.print("Enter desired character to find its index: ");
index = kb.nextInt();
String quote = ("Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time. Thomas A. Edison");
for (int i = 0 ; i<quote.length() ; i++)
{ if (quote.charAt(i) == ' ')
System.out.println(i);
}
}
}
You are reading index but never using it in the search logic...
I would change the type of index for a char instead AND compare the index with the char at index i
Example:
public static void main(String[] args) {
char index;
Scanner kb = new Scanner(System.in);
System.out.print("Enter desired character to find its index: ");
index = kb.nextLine().charAt(0);
final String quote = ("Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time. Thomas A. Edison");
for (int i = 0; i < quote.length(); i++) {
if (quote.charAt(i) == index) {
System.out.println(i);
}
}
}
You might try to replace the hard-coded space (' ') in your if statement with the input you get from the user.
Additionally, it just so happens that the String class in java has an indexOf method that will accomplish exactly what you're trying to do with the for loop.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter desired character to find its index: ");
char charToFind = in.nextLine().charAt(0);
String quote = "Our greatest weakness lies in giving up...";
System.out.println(quote.indexOf(charToFind)); // -1 if not found
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi i have a program that doesnt seem to be registering. I am fairly new so I would appreciate any help and there might be stupid mistakes made :/
But the point of the program is the enter a name and then find the name in a file called names.txt and then show the popularity of the name throughout the century. I currently have a program that doesnt seem to be working. Help Please
import java.io.*;
import java.util.Scanner;
public class Babynames{
public static void main (String[] args)throws FileNotFoundException{
Scanner reader = new Scanner(new File("names.txt"));
Scanner input=new Scanner(System.in);
System.out.print("What name would you like to search up: ");
String name = input.nextLine();
Scanner lineScan = new Scanner(name);
String thisname = lineScan.next();
if (name.equals(name))
{
while (lineScan.hasNextInt())
{
int next = lineScan.nextInt();
for (int i = 1900; i <=2000; i+=10)
{
System.out.print(i + next);
}
}
}
else
{
System.out.println("File not found! Try again: ");
String filename = input.nextLine();
Scanner lineScan2 = new Scanner(name);
}
}
}
Edit
it just asks for the name and after that the program ends
Your assignment seems to be:
Accept a baby name as input
find that name in a file that includes some information about that name
output the result which includes the info about the name.
From your code, I'm making an educated guess that a line in your file looks like:
name value value value value value value value value value value value
Where the values represent the popularity of the name 1900 - 2000 by decade (11 values)
So, your program would need to:
Get the user input (name) from System.in using a Scanner .
Open the file
Loop, reading a line from the file
Split the line by space (" ") into a String[] array
Compare the first element (array[0]) to see if it's your name
if it is, go through the rest of the array and output the values