Using the Scanner class to read a scanner string - java

I have a problem creating a student class which contains a constructor which takes a Scanner string of a format "Brookes 00918 X12 X14 X16 X21". The conditions should be that there should be a student name and student number and the course codes should start with an "X". I have thrown IncorrectFormatExceptions in the case that they are not satisfied. However when I create a test class and enter a string and press enter , for example "abc 123" it doesn't produce an output which is usually the case.
Update: I've changed the code to use a String array tokens however now with the toString() method using "123 abc X12" it gives a Null Pointer Exception. It works when I put "123 abc" in the constructor
Update:Seems to work now forgot to initialize the arrayList
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student extends UniversityPerson{
private String studentNumber="";
private List<String> courses=new ArrayList<String>();
private String studentName="";
public int checkNoofletters(char[] chararray){
int noofletters=0;
for (char c:chararray){
if (Character.isLetter(c)){
noofletters++;
}
}
return noofletters;
}
public String courseListinStr(){
String stringo="";
for (String c:courses){
stringo+=c;
stringo+=" ";
}
return stringo;
}
public Student(Scanner scanner) throws IncorrectFormatException{
int studentNumberCount=0;
int studentNameCount=0;
Scanner s=scanner;
String input=s.nextLine();
String[] tokens=input.split("\\s");
for (int i=0; i<tokens.length; i++){
char[] chars=tokens[i].toCharArray();
if (checkNoofletters(chars)==chars.length){//if the number of letters is equal to the character length
if (studentNameCount==1){throw new IncorrectFormatException("Can only have 1 student name");}
studentNameCount++;
this.studentName=tokens[i];
continue;
}
if (tokens[i].matches("[0-9]+")){//add to the studentNumbers list
if (studentNumberCount==1){throw new IncorrectFormatException("Can only have 1 student number");}
studentNumberCount++;
this.studentNumber=tokens[i];
continue;
}
if (!tokens[i].startsWith("X")){
throw new IncorrectFormatException("Course code must start with an 'X'");
}
System.out.println(tokens[i]);
courses.add(tokens[i]);
}
if (studentNumber=="" || studentName==""){
throw new IncorrectFormatException("Must have 1 student Number and Student Name");
}
}
#Override
public String toString() {
//return String.format("%s %s", studentName,courseListinStr());
return String.format("Student: %s %s", studentName,studentNumber);
}
#Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

The best way would be to do something like this:
Scanner s=scanner;
String input = s.nextLine();
String[] tokens=input.split("\\s");
Now you can test all your conditions:
if (tokens.size() < yourNumber) throw new Exception("add here");
if (tokens[2].charAt(0)!='X') throw new Exception("add here");
and so on; it should be rather easy to create your Student Object based on your requirements.

Your program is full of errors and I'll list some of them after answering why it doesn't print anything: if you dump all threads you'll see that the main thread is stuck at next(), which blocks until next token is available, and effectively never leaves the constructor of the first student
if (s.hasNextInt()){
studentNumbers.add(s.nextInt());
s.next();
continue; // <--------- this line
}
I think this is not the only error in your program, so maybe you'd better throw the entire parsing away and restart from scratch.
You should create exactly one Scanner object for each input stream, not one for parsed object
You should not pass the scanner to the Student constructor
You should not put any algorithm in a constructor: make a separate object instead
To simplify your program introduce a Parser class
public class Parser {
public Parser(Reader in) {}
public boolean hasNext() {}
public Student next() {}
}
and inside next() make the parser deal with entire lines (Scanner.hasNextLine and Scanner.nextLine()) instead of individual tokens, otherwise you'll have to define a custom protocol to mean EOR (end of record)
Dealing with lines is easier to think about, program and test. Once you have the full record, you can further tokenize it with a simple String.split(), or directly use regular expressions.

I didn't go through, your whole code. But, I would suggest you to use StringTokenizer or split function and store it in temp array. Then, traverse through your temp array and validate the data.

Related

String array list using Scanner

Edit: added the braces {} and an Eclipse screenshot
I have a problem with a simple assignment to create a class that has an array list to store words from the command-line, with each word appended to the end of the array list, then search the list for a specific word ("the") and prints location of the word's occurences, plus another method to return a new array list with the words at the positions specified by a number.
I wrote the code below. In Eclipse IDE, it doesn't show any errors, but when I tried to run the code by Run Configurations and enter this dummy text: "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.", it doesn't work. I'm not sure why, could you give me advice? Thank you!
import java.util.ArrayList;
import java.util.Scanner;
class Search {
ArrayList<String> list;
public Search(){
list = new ArrayList<String>();
}
public void read(){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
list.add(scanner.next());
}
scanner.close();
}
public void find(String word){
for(int i = 0; i < list.size(); i++) {
if(list.get(i).equalsIgnoreCase(word)) {
System.out.println(i);
}
}
}
public ArrayList<String> subsequence(int[] positions){
ArrayList<String> result = new ArrayList<String>();
for(int i = 0; i < positions.length; i++) {
if(positions[i] >= 0 && positions[i] < list.size()) {
result.add(list.get(positions[i]));
}
}
return result;
}
// Testing method within class again
public static void main(String[] args){
Search search = new Search();
search.read();
search.find("the");
for(String s : search.subsequence(new int[]{0, 3, 4}))
System.out.println(s);
}
}
As dear #Chris commented kindly use the braces while coding. I run your code in two different ways and I got the output.
First Way
If you want to use scanner.hashNext() with while then you should change your main() and read() like this
public static void main(String[] args) {
Search search = new Search();
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
sc.close();
search.read(line);
search.find("the");
for (String s : search.subsequence(new int[] { 0, 3, 4 })) {
System.out.println(s);
}
}
public void read(String line) {
Scanner scanner = new Scanner(line).useDelimiter("\\s");
while (scanner.hasNext()) {
list.add(scanner.next());
}
scanner.close();
}
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
The next() and hasNext() methods and their primitive-type companion methods first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext() and next() may block waiting for further input. Whether a hasNext() block has no connection to whether or not its associated next method will block.
Second Way
If you want to use StringTokenizer then you should have to change just read() to your existing code.
public void read(String line) {
Scanner scanner = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(scanner.nextLine(), " ");
while (st.hasMoreElements()) {
list.add(st.nextElement().toString());
}
scanner.close();
}
It is always better to use StringTokenizer for the string. The StringTokenizer class allows an application to break a string into tokens.
A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed. A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
Reference : Scanner and StringTokenizer

Why is my display() method not printing out all ArrayList elements?

I have a class Container where a user should be able to input any number of words until he types nothing. I have addWord(Word) method where each input is added to an ArrayList words every time do/while loop is run. I am passing user input value as a parameter to addWord() method each time loop runs.
Now I want to to display all elements of an array using display() method once the do/While loop has stopped running. But for some reason when i try to call method display(), it just shows an empty array [].
Is there any way you can help?
import java.util.ArrayList;
import java.util.List;
public class Container {
private List<String> words;
public Container() {
}
public List<String> getWords() {
return words;
}
public void setWords(List<String> words) {
this.words = words;
}
public void addWord(String word) {
words = new ArrayList<String>();
words.add(word);
}
public void display() {
System.out.println(words);
}
}
Main method:
import java.util.Scanner;
public class ContainerMain
{
public static void main(String[] args)
{
Container one = new Container();
Scanner myScan = new Scanner(System.in);
String word = "s";
do
{
word = myScan.nextLine();
one.addWord(word);
}
while (!word.equals(""));
if (word.equals("")) {
one.display();
}
else {
System.out.println("No hope fam");
}
}
}
Look at your addWord method:
public void addWord(String word) {
words = new ArrayList<String>();
words.add(word);
}
Each time you call that, it's going to create a new list - so you can never end up with more than one word in it.
The first line, initializing words, should be in your constructor (or as a field initializer). Then remove the line from addWord - ideally making the words field final at the same time, to avoid mistakes like this in the future, and remove the setWords method unless you really need it for something else.
That's all that wrong in Container (although it's not clear that it's really providing any value beyond just using a List<String> directly). As noted in comments, currently your do/while loop in main will add an empty string at the end. Also, there's no point in checking whether word is empty or not after the loop - it has to be, otherwise you wouldn't have exited the loop!

Scanner is requiring me to type inputs twice just for one to register

I've been doing a ton of research on this for the past few hours, with no luck. I am pretty sure this is a problem with .next() or .nextLine() (according to my searches). However, nothing has helped me solve my problem.
When I run the code below, I have to type in input twice, and only one of the inputs is subsequently added to the arrayList (which can be seen when you print the contents of the arrayList).
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Tester{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
if(console.next().equals("done")) break;
//console.nextLine(); /*according to my observations, with every use of .next() or .nextLine(), I am required to type in the same input one more time
//* however, all my google/stackoverflow/ reddit searches said to include
//* a .nextLine() */
//String inputs = console.next(); //.next makes me type input twice, .nextLine only makes me do it once, but doesn't add anything to arrayList
strings.add(console.next());
}
System.out.println(strings); //for testing purposes
console.close();
}
}
Problem with your code is that you are doing console.next() two times.
1st Inside if condition and
2nd while adding to ArrayList.
Correct Code :
public class TestClass{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
String input = console.next();
if(input.equals("done")) break;
strings.add(input);
System.out.println(strings);
}
System.out.println(strings); //for testing purposes
console.close();
}
}
In your code, you are asking for two words to be inserted. Just remove one of them.
Use it this way:
String choice = console.next();
if (choince.equals('done')) break;
strings.add(choice);

How to compare a string to a specific compartment of an array of strings?

I'm making a quiz like game. I have two arrays (One with the states and the other with the capitals). Basically it asks the user what capital goes with a random state. I want that if the user inputs the correct state for it to be like nice job or whatever but I do not know how to compare the user input to the specific array compartment. I tried .contains but no avail...
Any help?
My bad - I'm using Java
For Example
if(guess.equals(capitals[random]))
where guess is the string and capitals is the array and random is the random number
Basically you want a mapping String -> String (State -> Capital). This could be done using a Map<String, String> or by creating a State class which will contains its name and its capital as attributes.
But to my mind, the best option is to use an enum as you know that there is 50 states. Here's a small example.
public class Test {
static final State[] states = State.values();
static Random r = new Random();
static Scanner sc = new Scanner(System.in);
public static void main (String[] args){
State random = states[r.nextInt(states.length)];
random.askQuestion();
String answer = sc.nextLine();
if(answer.equals(random.getCapital())){
System.out.println("Congrats");
} else {
System.out.println("Not really");
}
}
}
enum State {
//Some states, add the other ones
ALABAMA("Montgomery"),
ALASKA("Juneau");
private final String capital;
private State(String capital){
this.capital = capital;
}
public String getCapital(){
return this.capital;
}
public void askQuestion(){
System.out.println("What capital goes with "+this.name()+"?");
}
}
Logic similar to this should work. You want to save the user input to a String variable, and then compare it to an n sized array.
for(int i=0; i<arrayName.length();i++)
{
if(userinputString.equalsIgnorCase(arrayName[i])
{
System.out.println("HUrray!");
}//end if
}//end for
Ok so you are somehow producing a random number, and then need to compare the input to the String in the capital Array for that random number/
Im assuming the arrays are ordered such that capitals[10] gives you the capital for states[10].
If so, just save the index to a variable.
int ranNum=RandomNumFunction();
Then just see if
if(capitals[ranNum].equalsIgnoreCase(userInput))
//do something

java calling a method from another class

I am working on a problem and I am very stuck because I am just starting to learn java. Any help I can get to understand this would be great. I have to write a program that has two classes. The main class will read from a file and uses the second class to find how may times the same words have been repeated in the file and add them to an array that contans the words and number of times the word repeated. I am ok with the reading the file part. I just can't seem to wrap my head around how to call a method from the second class to add the word into the array and increment the counter.
Here is my code so far if you run it you will see how new I am to this by how many errors you will get.
import java.io.*;
public class Words{
public static void main (String [] args)
{
ProcessInput();
System.out.println("\nprogram finished");
}
public static WordList ProcessInput( )
{
BufferedReader inputFile;
String inputLine;
String[] word;
WordList words;
try
{
inputFile=new BufferedReader(new FileReader ("inputFile.txt"));
inputLine = inputFile.readLine();
while (inputLine !=null)
{
word=inputLine.toLowerCase().split(" ");
for (int i=0; i<word.length; i++){
System.out.println (word[i]);
words=addWord(word[i]);
}
inputLine = inputFile.readLine();
}
inputFile.close();
}
catch (IOException ioe)
{
System.out.println (ioe.getMessage());
ioe.printStackTrace ();
}
return words;
}
}
class WordList {
String [] words;
int wordcount;
public WordList ( ){
words= new String [1000];
wordcount=0;
}
public String addWord (String word) {
words[wordcount]=word;
wordcount=+1;
return words[wordcount];
}
public void printList (){
for (int i=0; i<wordcount; i++){
System.out.println (words[i]);
}
}
}
You're very close. What you need to remember is when you're calling a method from another class you need to tell the compiler where to find that method.
So, instead of simply calling addWord("someWord"), you will need to initialise an instance of the WordList class (e.g. WordList list = new WordList();), and then call the method using that (i.e. list.addWord("someWord");.
However, your code at the moment will still throw an error there, because that would be trying to call a non-static method from a static one. So, you could either make addWord() static, or change the methods in the Words class so that they're not static.
My bad with the above paragraph - however you might want to reconsider ProcessInput() being a static method - does it really need to be?
You have to initialise the object (create the object itself) in order to be able to call its methods otherwise you would get a NullPointerException.
WordList words = new WordList();

Categories

Resources