Hi I have to write a program that takes in number of candidate and how many votes they got from the best to the worst. And I wrote two different classes to do that using a fileInputStream and another that uses the scanner class and storing it in the arraylist but the way the teacher has it in the text file is that some of the ballots are on different line so they will go like on one line while the ballots are on the other line. so its like this:
//this is how the text appears in the text file and I was wondering if I could get all
//the "votes" to look like the first one.
<v> 5 4 3 2 1
<v> 1 2 3 4 5
<v>
1
2
3
5
<v>
5
2
4
1
3
I think your teacher has done it deliberately. The trick here is to realize that the votes are not delimited by a new line, instead they are delimited by a "V". You can use this information along with regex(Pattern) to derive a solution.
If you don't want to use a regex, you could use Scanner.
if (scanner.hasNext("<v>")
scanner.next();//ignore the "<v>"
if (scanner.hasNextInt())
int vote1 = scanner.nextInt();//first vote
if (scanner.hasNextInt())
int vote2 = scanner.nextInt();//second vote
This would read for example:
<v> 1
4
I don't want to spoil the 'fun' of making your homework, so that's all. If you have more questions, just ask.
Related
I'm using 4 setter to allow user to set marks for 4 people as follows -
Marks marks = new Marks();
marks.setJames(1);
marks.setPeter(2);
marks.setRayn(3);
marks.setLayla(0);
Problem Statement -
I want a scenario where the user can only provide 0 marks only upto 3 people but not 4.
If user is trying to set 4th person with 0 marks after setting up 3 people's marks = 0 , then we need to throw an error of something like (Can't set 4 people to have 0 marks).
How can I achieve this? I know this maybe a silly question but I'm noob at java. Thanks in advance!
So I'm having a bit of trouble on one of my assignments. I can't seem to figure out what I need to do. Here is the question:
Points for reading are assigned on the following basis: The first three books read are worth 10 points each. The next three books read are worth 15 points each. All books read over six are worth 20 points each. A student who reads 7 books would be awarded 95 points (30 for the first three plus 45 for the next 3 and 20 for the 7th book).
An external file contains a first and last name followed by an integer (the number of books read)
Print on each persons name, the number of books read and the points earned. The names should be in the order last, first with the only the last name in all capital letters. At the bottom of the list print out the average points for all readers and the winner of the contest.
Statements Required: input, output, decision making, loop control, strings
Data Location: prog700c.dat
Sample Output:
Reading Contest
Name Books Points
SUMMER Sam 4 45
LAZY Linda 2 20
PRODDER Paul 5 60
MASTER K.C. 8 115
READER Richie 6 75
Average points per reader = 63.0
The winner of the contest is K.C. Master
The external file data is this:
SUMMER Sam 4
LAZY Linda 2
PRODDER Paul 5
MASTER K.C. 8
READER Richie 6
My current code:
import java.io.*;
import java.util.*;
public class Prog505a
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(new File("C:\\Users\\Guest\\Documents\\java programs\\Prog700c\\Prog700c.in"));
String data = kbReader.nextLine();
while(kbReader.hasNextLine())
{
I'm having a problem where I don't know what string method to use to only get the numbers from the external file to use them in the calculations. I know I can do the decision making, etc. but I just don't know what to do on this one part to get only the numbers from the external file. If someone could provide some direction or guidance would be greatly appreciated. Thank you!
You can use String.split("\\s+") to split the line into three parts (check if they are three parts to be sure, print a warning and skip if not). This will return an array of strings. You can use Integer.parseInt() to read the number of books on the third item in the array (position 2, arrays in Java are "zero based").
Note that "\\s+" is a regular expression which finds 1 or more parts of whitespace. The strings within the whitespace and the beginning/end of the string are returned. If regular expressions are not allowed, try and find the index of the space-character within the string, and use String.substring() - and don't ignore the return value for string operations.
Alternatively you could use next(), next() and nextInt() instead of nextLine() in your scanner.
You could use regex to deliver what you're saying :
Try to use something like:
str = str.replaceAll("\\D+","");
This way you would delete all non-digits in a string. After that you would need to parse the String to an integer or any other Number type.
I've made a parallel topic model using mallet.
And I want to get top-words for each document.
To do that, I'm trying to get a word-topic probability matrix.
How would I achieve this?
When you are building topics using MALLET, you have an option called --word-topic-counts-file. When you give this option and specify a file, MALLET writes ( topic, word, probability ) values per each line in the file. You can later read this file in C, Java or R (of course, any language) to create the matrix you want.
Just to make one point regarding the answer of Praveen.
Using the --word-topic-counts-file, MALLET will create a file which first few rows look something like this:
0 elizabeth 19:1
1 needham 19:2 17:1
2 died 19:2
3 mother 17:1 19:1 14:1
where first line means that the word elizabeth has been present in the topic 19 once; second line means that the word needham is associated two times with the topic 19, and with the topic 17 once; and so on...
Although, this file doesn't give you explicit probabilities, you can use it to calculate them.
to start with i want to say this is no homework or somthing, i just want deeper knowledge about these kind of arrays with I/O so feel free to just tell me how you tackle the problem WITH SCANNER, if its solvable :P
if i have a txt file that is like:
car 1 2 3 4 5
boat 1 2 3 4 5
plane 1 2 3 4 5
and i have made a new class in new .java-file which is an abstract 2d array:
class Type
{
String type;
int number;
}
public toString()
{
return String.format("%02d:%02d", type, number);
}
is it possible to get an outprint like:
car:1 car:2 car:3
boat:1 boat:2 boat:3
etc? thanks.
edit: also an ArrayList of course..
edit2:
while (scanner.hasNext())
{
list.add(scanner.hasNext(), 0); //the array should be <car, 0>
} //later i will loop through numbers
The pseudo-code of what I'm suggesting is this:
create line Scanner
while scanner has next *line*
get the next line
use String#split(" ") to create a String array from this line
Create your object from the items in this array
Add your new object into your list
end while loop
First thing that comes to mind is this (assuming your file will always have that structure):
Read your line and use string.split("\\s+"); to break your line into word tokens (\\s+ denotes one or more space in regular expression language).
If you can be certain that the word will always be the first element in the line, then, you can iterate from your 2nd token (your first number) till your last token (your last number) and with each iteration you create a new Type object where the type is the first token and the number is the nth token.
The above should allow you to construct objects which when printed, will yield your desired output.
If on the other hand, the order of the tokens is not known, but you are sure that in every line you will have one word and n numbers, you can use further regular expressions such as ^\\w+$ and ^\\d+$ to see which token is either a word or a digit respectively. Once you know which token is what, you can refer to the above points to get your code to work.
For more information on regular expressions, you can take a look at this tutorial.
I have the following data:
Maths abc10 4 def08 6 ;
English hrd45 3 ngh05 10 ; .
The word at the beginning is a keyword which is also in an enum, the following data are username and login pairs (there can be an unlimited number of these. The data for each keyword is terminated by ';' and the data is terminated by '.'
I'm using the scanner class but I can't get it to loop so that it can produce the following:
Maths abc10 4
Maths def08 6
English hrd45 3
English ngh05 10
Any Ideas? Thanks!
Store it all in a Map<String, Map<String, Integer> >. Iterate over the lines, grabbing the subject and then the name. (Loop starts here). Check if the name is ; and exit if it is. Grab the score, then add it to a Map<String, Integer>. Then grab the next name. (Loop ends here). Add the current Map<String, Integer> to the big Map<>, then do it all over again.
You could have a look at the regular expressions java.util.regex, if it's homework you'll learn about those as well which, trust me, it doesn't hurt. For things this simple, it's not worth going the parser way IMHO.