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
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 1 year ago.
Improve this question
Here is the code I have.
//input
System.out.println("Using numbers, in what month were you born? ");
String **userMonth** = input.nextLine();
// also tried int **userMonth** = input.nextInt();
It does not work either way.
userMonth will not "activate?" (sorry noob and don't know all terms)
When tried to call later in program error:
if (userMonth < 3) {
System.out.println("Your vacation home is a van down by a river!");
}
//userMonth cannot be resolved to variable
Very odd that your variables are showing up as **varName** instead of varName.
First use:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();
But it does not consume the newline ('ENTER').
Therefore, immediately after, consume the newline character via:
input.nextLine();
Also make sure Scanner was declared as
Scanner input = new Scanner(System.in);
Don't forget to close the scanner at the end of program
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 5 years ago.
Improve this question
I apologize in advance for my post. I decided to register tonight and this is my first post.
A little background about the question.
I am doing a little java project, where I need to take a name from user input, which is saved in a variable. the variable value must then display the output.
my question is. How is it done in java, using the (if) keyword, to use my code to create an if statement that voids my simple i_PlayerName method of any logic error.
to be more specific, how can I make sure that my program will only accept a string value, and if another data type is entered, an output message will be displayed " user input not accepted ".
lastly, what is this procedure called?
Thank you all :)
keep on coding the code.
public void i_PlayerName() {
String playerName;
Scanner i_playerName = new Scanner(System.in);
System.out.println("ENTER YOUR SHIP NAME");
playerName = i_playerName.nextLine();
System.out.println("Your vessel has been named " + playerName);
}
Assuming you meant alphabets when you said only strings are allowed, this should do it.
public static void main(String[] Args) {
String playerName = "";
System.out.print("Enter ship name: ");
Scanner scanner = new Scanner(System.in);
String i_playerName = scanner.next();
System.out.println();
char[] chars = i_playerName.toCharArray();
boolean isChar = true;
for (char c : chars) {
if (!Character.isLetter(c)) {
isChar = false;
}
}
if(isChar == true) {
System.out.println("Your vessel has been named: " + i_playerName);
}
else {
System.out.println("User input is not accepted.");
}
}
when the scanner is reading if you assign it to a String type it will take the input as a String regardless if the user inputs numbers or Strings. If you want to handle the scenario when user inputs value other than String you can do it with regular expression and if the input is not a match then output the error. Use below method to check if input is a String.
public boolean isStringValue(String s){
String pattern= "[a-zA-Z]*";
return s.matches(pattern);
}
//use method above:
String input = scan.next();
if(isStringValue(input)){
//your code
} else {
//output some error
}
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to Java but quite good at C. I want to take the input from user in Java. Could anyone please suggest the basic concept with the exact code? Thanks
import java.util.Scanner;
public class Main {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String inputText = sc.nextLine();
}
}
import java.util.Scanner;
This imports the java library that will allow you to read data from the specified input source.
Scanner sc = new Scanner(System.in);
Gets a working instance of the Scanner library in sc which will go get the input for you from the standard input of the system, System.in, in our case, which is the keyboard.
String inputText = sc.nextLine();
This code asks the sc instance to get the line of text typed by the user and set it into the inputText variable.
Note that when sc.nextLine() is called, it halts any execution and waits for user input and proceeds only when the user presses the Enter on the keyboard.
Here is a very simple idea using a Scanner
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter something searched.. ");
String input = scan.next();
System.out.println("your input is " + input);
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Like topic says I'm looking for the best way to:
I got .txt file. In this file there are for example:
Matthew Sawicki 25\n
Wladimir Putingo 28\n
Barracko Obamaso 27
Whats the best way to write a program that opens this file, checks out the biggest number and then prints out that?
I was thinking about: open file -> check each line with hasNextLine method saving the biggest number (addin i for measuring the lines - 1, 2, 3) and then close file and open again and then somehow prints out that line
Ok there goes edit then.
By the way I have to write name of file in console to open it. And I have to use Scanner.
My code:
Scanner scanner= new Scanner (System.in);
File file = new File (scanner.nextLine);
scanner = new Scanner (file);
int temp=O;
int i=0;
while (scanner.hasNextLine) {
String word1=scanner.next;
String word2=scanner.next;
String word3=scanner.next;
If(word3>temp)
temp3=word;
i++; // now i get the i id of the line with the biggest number
And now I'm thinking bout reopen file and loop again to print out that line with the biggest number(By for instance if(newWord3==temp))
is it a good idea? And how to reopen the file? can anyone continue the code?
Assuming that this file will always be in the same format here's a snippet that does what you want with no checking to make sure that anything is in the wrong place/format.
//Create a new scanner pointed at the file in question
Scanner scanner= new Scanner(new File ("C:\\Path\\To\\something.txt"));
//Create a placeholder for the currently known biggest integer
int biggest = 0;
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
//This line assumes that the third word separated by spaces is an
// integer and copies it to the `cndt` variable
int cndt = Integer.parseInt(s.split(" ")[2]);
//If cndt is bigger than the biggest recorded integer so far then
// copy that as the new biggest integer
biggest = cndt > biggest ? cndt : biggest;
}
//Voila
System.out.println("Biggest: " + biggest);
You'll want to verify that the number in question is there, and that you can handle the case that a line in your text file is malformed
There are several ways to do what you want. The way you described can work, but as you noted it requires to scan the file twice (in the worst case, which is when the row you have to print is the last one).
A better way which avoid to reopen the file again is to modify your algorithm to save not only the biggest number and the corresponding row number, but saving the whole line if the number is bigger than the one previously saved. Then, when you're done scanning the file you can just print the string you saved, which is the one containing the biggest number.
Note that your code will not work: the if is comparing a String with an int, and also there's no temp3 variable (that's probably just a typo).
To follow my suggestion you should have something like this:
int rowNumber = Integer.parseInt(word3);
if(rowNumber > temp) {
temp = rowNumber;
tempRow = word1 + " " + word2 + " " + word3;
}
then you can just print out tempRow (which you should define outside the while loop).
It's all fine now. I've made a simple mistake in my file (an empty enter at the end) so i coudnt figure out how to do it.
Thanks for ur effort and gl!.