How to make Scanner read more than one line? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have a text corpus, which I have to read, split, sort and perform other operations on it.
In the very beginning, when I split it, I see that the Scanner only reads one line. This is the code:
public class CorpusTest {
public static void processCorpus(Scanner scanner) throws IOException{
String line="0";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
}
String[] w = line.replaceAll("[^a-zA-Z\\s]","").toLowerCase().split(" ");
for (int i = 0; i < w.length; i++) {
w[i].trim();
}
System.out.println("Word" + "\t" + "Frequency");
System.out.println(Arrays.toString(w));
}
public static void main(String [] args) throws IOException{
File temp = new File("input.txt");
Scanner scanner = new Scanner(temp);
CorpusTest.processCorpus(scanner);
}
}
I tried adding:
String text = new Scanner( new File("input.txt") ).useDelimiter("\\A").next();
But I get errors because in the method above I am working with an array.
The while loop only reads the last line, which is no good.

I'm not sure what your issue is, and it seems as if you might be trying to make things more difficult than they need to be. Why not simply read your lines in with the Scanner, one at a time, put them into a StringBuilder, and then when the text has been read in, convert to a String and manipulate your String to your hearts content?

#user2864740 helped me out with redirecting me to the right source. I used this instead of the loop in the beginning of my code:
String content = new Scanner(new File("input.txt")).useDelimiter("\\Z").next();
String[] w = content.replaceAll("[^a-zA-Z\\s]","").replaceAll("\n","").toLowerCase().split(" ");
Now it works.

Related

I can not get my code to run due to a run time error when trying to assign the String userAns to input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I'm writing a simple quiz program. The code on line 19 is giving me a run time error. Could someone please advise why?
import java.util.Scanner;
public class javaQuiz {
public static void main(String[] args) {
String questionOne = "Who is the best band member of the beatles?";
String questionOneAns = "John";
String questionTwo = "what is 1 + 1?";
int questionTwoAns = 2;
String questionThree = "What continent is China a part of?";
String questionThreeAns = "Asia";
String questionFour = "Who is the Turing Test named after?";
String questionFourAns = "Alan Turing";
Scanner userI = new Scanner(System.in);
String userAns = scan.NextLine();
System.out.println(questionOne);
if(userAns == questionOneAns) {
System.out.println("Correct!");
} else {
System.out.println("Wrong answer!");
}
}
}
Isnt it because your scanner is called userI instead of scan?
Line 19 should be:
String userAns = userI.nextLine();

Why `.read()` Method of a number return different number [closed]

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
Please explain the use of System.in.read() method in this example that I'm learning about from another post. As a beginner, I find it unclear when I input a number and get different one in the output, please clarify.
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
int inChar;
System.out.println("Enter a Character:");
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
System.in.read() reads values as their binary value; for example reading "a" will result in the value of "97" - the mapping of this is available here https://www.asciitable.com/.
In order to get the textual representation of this in Java you want to read this as either a Character or a String - Character is a single value, while a String is a combination of Characters one after the other. For example:
public class MainClass {
public static void main(String[] args) {
System.out.println("Enter a Character:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.print("You entered ");
System.out.println(input);
}
Have a look at the Scanner class to see other options, you can use scanner.nextInt() to get an Integer back.

Unable to print all the characters of a string by charAt() in Java? [closed]

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
I have a question. I am a beginner in programming and I have been stuck here for hours now. I want to know how you can read all the letters in a certain user input.
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
nameInput = naem.charAt(0); //where nameInput is a char
System.out.println("Name: "+nameInput);
I am making a program where have to take input a name and all the characters of that name will be printed through System.out.println("Name: " +nameInput);
I'd really appreciate it if you could help out.
You want to print the naem which you are receiving by the input and not a single character which is what you'd get if you used .charAt().
nameInput = naem.charAt(0); //1
System.out.println("Name: "+nameInput);//2
What actually these two lines are doing is that :
Line 1 is taking the first character of the string and storing it into nameInput and then line 2 is printing it.
Now if you want to print all the character in this way you have to go through the entire string. You can do this by using loop.
Here is an Example :
String naem = inPut.nextLine(); //where inPut is a "Scanner inPut = new Scanner (system.in)
System.out.println("Name: ");
for (int i = 0; i < naem.length(); i++)
{
char nameInput = naem.charAt(i);
System.out.print(nameInput);
}
By naem.length(), you will get the length of the string.

how to use arrays in methods [closed]

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 8 years ago.
Improve this question
Every time I try running this I get a java exception code. Any Ideas?
package employ;
import java.util.Scanner;
public class Employ {
public static void main(String[] args) {
String []empna={};
int numofemp;
int []empnu;
String []empadd;
int []emphd;
//Scanner sc = new Scanner(System.in);
System.out.println("how many employees do you have?");
Scanner sc = new Scanner(System.in);
numofemp=sc.nextInt();
for (int j=0;j<numofemp;j++){
empnam (empna,j);
// System.out.println(empna[0]);
}
}
public static void empnam(String empna[], int j ){
System.out.println("What is your employees first and last name?");
Scanner n = new Scanner(System.in);
//String ns=n.nextLine();
empna[j]=n.nextLine();
}
}
You didn't initialize the array with the correct size, this line is wrong:
String []empna={};
Try this instead, right after the line where you read the value of numofemp:
String[] empna = new String[numofemp];
Remember, an array in Java is of fixed length and its size must be specified at the creation time, it won't grow as elements are being added to it. If a variable-length array were needed, then use an ArrayList.

Chopper Java: what is wrong with this? [closed]

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 8 years ago.
Improve this question
import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers");
Scanner chopper = first Scanner();
int x =chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
This keeps telling me that a ';' is expected on the line that starts with scanner chopper, what could the problem be?
P.S. Do you know how i can get it to keep count of how many integers were typed in?
You have written first Scanner() instead of new Scanner(System.in).
You need to specify an input source, and since you want the user to input the numbers, you should use System.in as argument.
This line:
Scanner chopper = first Scanner();
should be:
Scanner chopper = new Scanner(System.in);
new Scanner(System.in) creates a new Scanner object that take input from the console, first Scanner() is syntactically incorrect and is what gives the error.
Why does your code say first Scanner(); instead of new Scanner();? I think that's the problem.

Categories

Resources