Reputation: 115
I have a program for an assignment that I cannot puzzle out the final tidbit.
It needs to be able to accept a large snippet of "DNA" code. The samples given are in the 100,000+ range. I wrote at first what wold work for a small sample, one line, wonderfully.
The TA told me that I should be able to add a while (input.hasNext())
and it will complete more than just the first line of the sample file I am copying and pasting into the console. It certainly does that! It just won't end. I tried using a break;
where I thought would be appropriate but end up back to where I was with just a single line being counted.
Scanner scan = new Scanner(System.in); //Scanner
System.out.println("Enter a DNA sequence consisting of A, T, G, and C, on one line: "); //Instructions for user.
dnaSequence = scan.nextLine(); //Scan for next line of string.
dnaSequence = dnaSequence.toUpperCase(); //Converts all letters entered upper case to avoid issues.
while(scan.hasNext()){
for (int i = 0; i < dnaSequence.length(); i++) //Make i = 0, i has to be less than the length of the entered sequence, will excute count.
{
validCount = dnaSequence.charAt(i); //[FILL IN!]
switch (validCount) //Switch for all valid counts
{
case 'A' : //For any case A.
countA++; //Count all As.
break;
case 'T' : //For any case T.
countT++; //Count all Ts.
break;
case 'C' : //For any case C.
countC++; //Count all Cs.
break;
case 'G' : //For any case G.
countG++; //Count all Gs.
break;
}
}
totalCountGC = countG + countC; //Math for G and C, together.
totalCountSequence = countA + countT + countG + countC; //Math for total count of all other counts in switch.
Upvotes: 0
Views: 1433
Reputation: 10613
You never consume any of the input inside the loop. The only time you actually read new data is before you enter the while loop, on this line:
dnaSequence = scan.nextLine();
So basically all you're doing is reading a single line from your input, and then proceeding to do your calculations on that same line over and over.
Move that, and the toUpperCase
inside the loop, and it will continue to read new lines in, and eventually consume all the input.
So your code would look something like this:
while(scan.hasNextLine()){
String dnaSequence = scan.nextLine().toUpperCase();
for (int i = 0; i < dnaSequence.length(); i++){
validCount = dnaSequence.charAt(i);
switch (validCount){
case 'A' :
countA++;
break;
case 'T' :
countT++;
break;
case 'C' :
countC++;
break;
case 'G' :
countG++;
break;
}
}
}
In this, I'm assuming that you're using something like input redirection from a file for your input, and not typing in the lines manually. If you do actually type them in at run time, then this won't work, since there's no way for the program to know when you're done.
Upvotes: 3
Reputation: 124215
System.in
is a input stream which usually
Now since it is possible that user is in the middle of creating data which will be send to that stream (he can be writing it in console but didn't press enter yet) hasNext
needs to wait with decision until
To avoid this problem you can let user provide some sort of special value which will end loop like
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if ("EXIT".equals(line))
break;
//if we are here it means loop should continue
handle(line);//or other code which will handle your data
}
To avoid infinite waiting on opened stream (like System.in
), you can choose other option. Simply make Scanner read from source which has end, like File.
So your code can look like:
//input.txt should contain data you want to read
File data = new File("path/to/your/input.txt");
Scanner scanner = new Scanner(data);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
//handle this line
}
Upvotes: 0