I am reading a binary file and I have to parse through the headers in this file.
I have a DataInputStream set up and I need to get it to continue looping through the file until the end. I am new to Java so not sure how I would carry this over from my C# experience. At the moment I have the following code:
while (is.position != is.length) {
if ( card == Staff) {
System.out.print(card);
} else if ( card == Student ) {
System.out.print(card);
} else if (card == Admin) {
System.out.print(card);
} else {
System.out.print("Incorrect");
}
}
is is the input stream I created, the only error I have is in the first line where the while loop starts under position and length it says they cannot be resolved or is not a field.
Looking at the docs it doesn't look like DataInputStream has a position field. You could possibly use one of the other methods to check whether there is more data available, but it's not clear from your code sample what you're trying to do.
At present there are a number of issues I can see with your code:
If card, Staff, Student and Admin are of type String, then you need to compare them using the equals(String s) method, not the == reference equality (ie. card.equals(Staff) rather than card == Staff)
You don't seem to iterate in your loop. If you don't do anything to change the value of is.position (I know this doesn't actually exist, but hypothetically speaking...) then if you can enter the loop you'll never leave it.
You don't change the value of card. If you do iterate some fixed number of times, you're going to just have identical output printed over and over again, which probably isn't what you intended.
Related
In my code, I am trying to filter out lines from a file that met conditions that are written by the user. A simple example would just be saying "red&round", and if written correctly, it would print out "apple". however, right now I am mentally stuck on how to simplify coding this. As of right now, I am only able to print out strings that match one condition, and I end up with either multiple of one string or strings that only match one condition. I feel like I know that I can just make another for loop within the for loop to filter it out eventually, but I feel like that would be a waste of time. Additionally, I have also tried to use while loops but I always ended up with infinite loops. This is my current code (only a snippet, where the main problem is). I am also just starting out in my first java course, and I would really appreciate any hints as to how I can make a simpler code for this problem.
if (args[1].contains("&")) {
String[] queryM = args[1].split("&");
for (int i = 0; i < queryM.length; i++) {
if (queryM[i].contains("not")) {
String command = queryM[i].substring(4, queryM[i].length() - 1);
if(run(lines.get(lineIndex), name(command), cond(command)) == false) {
System.out.println(lines.get(lineIndex));
}
}
else {
if(run(lines.get(lineIndex), name(queryM[i]), cond(queryM[i])) == true) {
System.out.println(lines.get(lineIndex));
}
}
}
}
I am working on a program which uses jLabels and I need to check if label is empty or not. If it's empty it should just pop up a note that it's empty and nothing else, but it actually throws a lot of errors. I'm using label.getText().isEmpty().
Here's the code:
if(Integer.parseInt(najboljsi1.getText())<1||Integer.parseInt(najboljsi1.getText())>17||najboljsi1.getText().isEmpty()||
Integer.parseInt(najboljsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||najboljsi2.getText().isEmpty()||
Integer.parseInt(najboljsi3.getText())<1||Integer.parseInt(najboljsi3.getText())>17||najboljsi3.getText().isEmpty()||
Integer.parseInt(najslabsi1.getText())<1||Integer.parseInt(najslabsi1.getText())>17||najslabsi1.getText().isEmpty()||
Integer.parseInt(najslabsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||najslabsi2.getText().isEmpty()||
Integer.parseInt(najslabsi3.getText())<1||Integer.parseInt(najslabsi3.getText())>17||najslabsi3.getText().isEmpty())
{
jLabel101.setForeground(Color.red);
jLabel101.setText("Eno ali več vnesenih števil ni v pravilnem obsegu (1-16)!");
}
else
{
jLabel101.setText("");
int a=Integer.parseInt(najboljsi1.getText());
tabela[a-1]+=3;
int b=Integer.parseInt(najboljsi2.getText());
tabela[b-1]+=2;
int c=Integer.parseInt(najboljsi3.getText());
tabela[c-1]+=1;
int d=Integer.parseInt(najslabsi1.getText());
tabela[d-1]-=3;
int e=Integer.parseInt(najslabsi2.getText());
tabela[e-1]-=2;
int f=Integer.parseInt(najslabsi3.getText());
tabela[f-1]-=1;
najboljsi1.setText("");
najboljsi2.setText("");
najboljsi3.setText("");
najslabsi1.setText("");
najslabsi2.setText("");
najslabsi3.setText("");
count++;
jLabel1.setText("Učenec "+count);
}
Everything else in if statement works ok, if value is lower than 1 or higher than 16, it throws a pop up.
Yes, you must test najboljsi1.getText().isEmpty() BEFORE any parsing of najboljsi1.getText().
Your if would become:
if(najboljsi1.getText().isEmpty()||Integer.parseInt(najboljsi1.getText())<1||Integer.parseInt(najboljsi1.getText())>17||
najboljsi2.getText().isEmpty()||Integer.parseInt(najboljsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||
etc...
If you do Integer.parseInt(najboljsi2.getText()) on a label with the textn "" (empty String), it won't be an integer. An exception will be thrown.
I think your problem is in the use of the "Integer.parseInt" without any check! If, for example, the variable contains an empty string, it will throw an Exception and your if clause will never work!
I would manage the situation with a double check.
Check if it is already a number (this guide could help
check-if-variable-is-a-number-in-javascript)
Then, if it is a string, check if it is empty and if it actually contains a string (the following post could also help check-whether-an-input-string-contains-a-number-in-javascript)
Ps. Sorry, I modified the answer with some extra links
I would like to get the highest score group by Id .If two highest score's are same then i would like get the highest score based on lowest Optional ID.I would like to get it in Java Stream.So far this code works.Is there any efficient way to rewrite this code in java stream
Example :
records=record.Person(batchNumber);
List<Person> highestRecords = new ArrayList<>();for(
Person s:records)
{
if(!highestRecords.isEmpty()) {
boolean contains = false;
for(Person ns: new ArrayList<>(highestRecords)) {
if(s.Id().compareTo(ns.Id()) == 0) {
contains = true;
if(s.getScore.compareTo(ns.getScore()) > 0
&& s.optionalId().compareTo(ns.optionalId()) < 0) {
highestRecords.remove(ns);
highestRecords.add(s)
}
}
}
if(contains == false) {
highestRecords.add(s);
}
}else {
highestRecords.add(s);
}
}
}
Don't convert this to a stream.
There is no one pure operation happening here. There are several.
Of note is the initial operation:
if(getNewPendingMatches.size() > 0)
That's always going to be false on the first iteration and you're always going to add one element in.
On subsequent iterations, life gets weird because now you're trying to remove elements while iterating over them. A stream cannot delete values from itself while iterating over itself; it only ever processes in one direction.
As written this code should not be converted to a stream. You won't gain any benefits in doing so, and you're going to actively harm readability if you do.
I'm new to JAVA and am struggling with a specific issue that I couldn't find an answer to so I decided to ask the good people of StackOverflow.
I am trying to build an ATM program. I have created an array of objects (The clients of the bank) with properties such as cardnumber, pin, balance, etc. And when any user will try and "log in" to the ATM he should type in his card number.
In the code below I tried to simulate the ATM going through the array of Clients and checking the input number with all the existing card numbers of each customer until it finds a match, and it works fine. The problem is that if it finds a match with client at position 10 it will display "Inexistent Card" 10 times before succeeding.
So I wanted to ask if there is a way for the program to ignore all these mismatches and only continue if it finds a match. And give me the "Inexistent Card" only when it finds 0 matches.
for (i = 0; i < clients.length; i++) {
if (input.equals(clients[i].accountnumber)) {
System.out.println("Welcome");
} else {
System.out.println("Inexistent Card");
}
}
consider using a found boolean variable
boolean found = false;
for (i = 0; i < clients.length; i++) {
if (input.equals(clients[i].accountnumber)) {
System.out.println("Welcome");
found = true;
break;
}
}
and then after the loop
if (!found) {
System.out.println("Inexistent Card");
}
That is exactly what you've written!
Think about it a bit more: You don't know the account doesn't exist until you've searched all the accounts and not found the one you want. You need to organise your code the same way. Just because clients[0] is not the one you are looking for, clients[1] might be, so you can't say "non existent" just yet...
The most simple way to achieve this:
int indexOfMatchingCard = -1;
for (... {
if (match) {
indexOfMatchingCard = current index
Afterwards, you can check if indexOfMatchingCard is >= 0; at the same time it tells you which of your cards matched. Just make sure to not reset that marker if you later find that other cards are not matching!
If you don't need to remember the matching index; you can use a simple boolean marker (which you initialize to false once; and change to true when on match).
Ok i don't know how to exactly explain completely what my issue is i'm facing to get what i want, but the basis of what i'm trying to accomplish here is...i don't want a -> ; <- to show up if the variable is Null or 0. Something I've attempted so far is a scanner input where when you run the code it asks to input values that are > 0 and if you input one thats not it'll give an invalid input error. Im trying to find a different method where its not needed to keep repeating this method for 20 or more. Like i said im just trying to have it input the numbers automatically, and if theres no number in one of the variables it would skip it and not put another " ; " and just put the ones that do have numbers with the semicolon. So what i'm looking at to accomplish is listed in the image bellow :
I had difficulties to understand your question. I am also not an english speaking person....
If I understood you want this:
String a,b,c,d,e, all;
all = "";
if(a!=null && a!=0){
all += a;
}
if(b!=null && b!=0){
all += b;
}
if(c!=null && c!=0){
all += c;
}
...
if(all != ""){
//something
} else {
// something else
}
Note that I am not permutating, I am sequencially checking for values in each variable then performing the desired effect.... I just concatenated strings, if you want to add stuffs like (space),; (semicolon), its up to you.