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.
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 5 years ago.
Improve this question
I'm trying to read integers from a file in Java and display the highest value, but I'm having a difficult time thinking about it.
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class MyFile
{
public static void main(String[] args) throws Exception
{
String filename;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the file.");
filename = in.nextLine();
File file = new File("myFile.txt");
Scanner inputfile = new Scanner(file);
while(inputfile.hasNext())
{
int compare = inputfile.nextInt();
}
}
}
The file is simply called myFile.txt and has the integers 23, 34, 45, and 2.
if you require to find the highest value from the file, you can do:
int max = Integer.MIN_VALUE;
while(inputfile.hasNext()) {
int compare = inputfile.nextInt();
if (compare > max){
max = compare;
}
}
System.out.println("highest value:"+ max);
Is this what you are looking for?
Declare a variable maxValue. Initialize this value with the file value in file. Loop through rest of the file comparing the newly read value with the maxValue. If the newly read value is greater than the maxValue. Assign maxValue to that value.
Javadoc states:
A Scanner breaks its input into tokens using a delimiter pattern,
which by default matches whitespace. Your file should contain
23 34 45 2
without commas in between
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 6 years ago.
Improve this question
Example code:
import java.util.Scanner;
public class Split {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a few words: ");
String wordsWhole = scan.next();
String[] wordsSplit = new String[4];
wordsSplit = wordsWhole.split("//s+");
System.out.println("Second word: " + wordsSplit[1]);
}
}
The output:
Enter a few words: Why no work
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at test.Split.main(Split.java:12)
My String isn't splitting into the array like I would expect it to. Any ideas on why this is?
Line 12:
System.out.println("Second word: " + wordsSplit[1]);
There are several problems:
Scanner.next() will only return the first word (space-separated) in the input, use Scanner.nextLine() to get the entire line.
I'm guessing you're trying to split by spaces. If so, you should use backslashes rather than forward slashes in your regex ("\\s+").
You don't need to allocate the array before assigning it to the result of the split. Just use String[] wordsSplit = wordsWhole.split("\\s+");
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();
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
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 9 years ago.
Improve this question
This my code
import java.util.*;
import java.util.Collections;
public class Customer
{
public static void main(String args[]){
Arraylist listcustomer1 = new Arraylist();
Arraylist listcustomer2 = new Arraylist();
Scanner scan = new Scanner(System.in);
String name,city;
int custId,numOfPurchases;
for(i=0;i<30;i++)
{
System.out.println("Enter customer name : ");
name = scan.next();
System.out.prinln("Enter customer id :" );
int custId= scan.nextInt();
System.out.println("Enter number of purchases :");
int numOfPurchases = scan.nextInt();
System.out.println("Enter the city :");
city = scan.next();
Customer a.new Customer(name,custId,numOfPurchases,city);
listcustomer1.add(a);
}
int total =0,avg = 0;
for(int i=0;i<listcustomer1.numOfPurchase;i++)
{
total= total+numOfPurchase;
avg = total/listcustomer1;
if(listcustomer1.numOfPurchase<10){
listcustomer1.remove(i);
Collections.copy(listcustomer2,i);
}
}
System.out.println("Customer Purchase Information ");
System.out.println("Total number of purchases from all cities " +total());
System.out.println("Average number of purchase from all cities " +avg());
}
}
I got this error after i running it :
Customer.java:27: error: ';' expected
Customer a.new Customer(name,custId,numOfPurchases,city);
Is this error for missing semicolon? I already put but the error still there.
Remove . from this line and add =
Customer a.new Customer(name,custId,numOfPurchases,city);
should be:
Customer a = new Customer(name,custId,numOfPurchases,city);
Customer a = new Customer(name,custId,numOfPurchases,city);
replace dot with equals symbol
Customer a.new Customer(name,custId,numOfPurchases,city);