I'm writing a program to take items from a given .txt file and put them into an array, then from that array, depending on the number, print it out as a character. For some reason it continually prints 3 spaces, then 8 at symbols, creates a new line and repeats, infinitely. I've tried a switch statement (which is commented out) but it did the same thing. cloudberry is the array in which it reads from, and it contains all the correct numbers.
while (read.hasNextLine()) {
int nutmeg = 0;
for (int x = 0; x < cloudberry.length;x++ ){
for (int y = 0; y < cloudberry[x].length;y++){
nutmeg = cloudberry[x][y];
switch (nutmeg){
case 1:
System.out.print("#");
break;
case 2:
System.out.print("+");
break;
case 3:
System.out.print(" ");
break;
}
}
}
}
Your code checks whether there is a new line but you never actually read it, therefore there is always the same line to read and the outer loop is iterating infinitely.
The code should be probably something like this:
while (read.hasNextLine()) {
String line = read.nextLine();
...
}
Related
I am making a Bingo game in C for a project and have made it through the initial board creation:
B = 1-20, I = 21-40, N = 41-60, G = 61-80, O = 81-99.
I have also been able to randomly generate a letter/number pair that doesn't repeat that is to be used to play the game per the following:
int main()
{
srand(time(0));
int rollcount = 0, rollc, rollr, nohund, basec=5, base=20;
const char BINGO[5] = {'B','I','N','G','O'};
bool check[101];
//fills check array with falses that will change to true as numbers are picked.
for(int i = 0; I < 100; i++)
check[i]=false;
//marks free space as true, free = 100 on board
check[100]=true;
do
{
//test var to prevent duplicate "rolls" until a game is won
bool reroll=true;
rollcount++;
//here's where the problem starts
//This is meant to loop if the number rolled has been rolled before.
do
{
//getchar();
if(getchar() == 'q')
{
//future code to display stats and escape game
return 0;
}
rollc = rand() %basec; //pick random column
//pick random number in range of selected column
rollr = rand() %base + 1;
rollr += (rollc * 20);
//limits last col to 99 instead of 100
nohund = (rollr == 100 ? rollr -= 1 : rollr);
if (check[rollr] == false) //checks if number has been used
{
//prints the number of the roll, column letter, and random number
printf("%d: %c-%d", rollcount, BINGO[rollc], rollr);
check[rollr]=true; //prevents future use of that number
reroll=false; //escapes loop to roll another number
}
}
//roll current roll again when duplicate is generated
while(reroll==true);
}
//intended to roll with each press of 'ENTER'
while(getchar()=='\n');
}
I have tried a few things. As written above, the major problem is if a duplicate is found, it shows the blank line from the getchar() input in the if() statement. As more numbers are found, wider and wider gaps between lines appear (due to needing more ENTER presses to find a unique number), leaving a mostly blank screen. With a single ENTER press, I need it to loop until it finds a good number. If I get rid of all getchar() expressions, the program will do this, and I can press ENTER until I get 99 unique numbers, but I need to allow to press q to quit the program.
Example Output
1: B-15
2: G-78
3: I-37
4: G-62
To summarize, I need it to display numbers like it does without getchar(), make it through all 99 possibilities, and let me quit with q.
Please help, and thank you in advance.
Let's first apply some fixes to other parts of the code, to make sure it all works as intended. This is the typical signature for a main function without parameters:
int main(void)
The middle I here needs to be lowercase, otherwise the code won't compile or it will use the imaginary number from the complex numbers:
// Note: index 0 is not used
for(int i = 1; i < 100; i++)
The most direct way of making the program work, would be to move the getchar() == 'q' check to before the do-loop. The function of the do-loop is now to loop until it finds a number which hasn't been chosen before. The problem is now that this will loop forever if all numbers are taken. Therefore, we'll add an additional check: only loop if there are still numbers available.
if(getchar() == 'q')
{
return 0;
}
bool aNumberIsAvailable;
do
{
// ... same code as before ...
aNumberIsAvailable = false;
for(int i = 0; i < 100; i++)
{
if (!check[i])
{
aNumberIsAvailable = true;
break;
}
}
}
while(reroll==true && aNumberIsAvailable);
That said, there are better ways to design this program. One simple step is to combine the two getchars into one, as if it is a menu: A '\n' result means "roll another number" and a 'q' result means "quit". As user3386109 suggested, there are better ways to solve the "sample without replacement"-problem.
Finally, note that getchar does not detect key presses. It simply reads a single character from the (terminal) input buffer. If you would like see actual key up/down movements, you will need a library which gives you more direct access to the keyboard such as SDL2.
I am attempting to solve this problem but I'm not sure why my solution doesn't work. My attempts at debugging tell me that the solution is attempting to access indices outside of the bounds of some of the data structures, but this does not make sense to me as it seems like my for-loop test would would.
There are probably many other issues with this solution besides this.
I'm also 90% sure that there's a more efficient way to do this. Could you help me figure out what it is I've done wrong here?
If there is a more efficient solution, what would it be? I'm struggling to deal with keeping track of the same number of spaces in the same order in an efficient way.
If any more information is necessary, please let me know and I will update.
public static void printReversed(String line){
Scanner console = new Scanner(line);
ArrayList<String> list = new ArrayList<String>(); // keeps track of words in line
int spaceOccur = 0; // keeps track of the number of times there are spaces
while (console.hasNext()){
list.add(console.next());
spaceOccur++;
}
int[] spaces = new int[spaceOccur]; // keeps track of number of spaces for each occurrence of spaces
int count = 0; // for spaces[] traversal
// searches through original input to get number of spaces
for (int i = 0; i < line.length() - 1; i++){
if (line.charAt(i) == ' '){
int j = i;
int num = 0;
// traversal through spaces to count how many
while (line.charAt(j) == (' ')){ // first error here
num++;
j++;
}
i = j; // updates for loop counter to point past spaces
spaces[count] = num; // saves number of spaces
count++;
}
}
// printing reversed input
for (int k = 0; k < list.size(); k++){
// prints reversed chars
for (int m = list.get(k).length(); m > 0; m++){
System.out.print(list.get(k).charAt(m));
}
// prints spaces
for (int n = 0; n < spaces[k]; n++){
System.out.print(" ");
}
}
}
I'd say that you're on right tracks, but some places need some closer inspection. The first loop seems to have some problems: The j++ is probably the one that goes beyond boundaries of the array - at least if you have spaces at the end of your string. And the whole loop itself seems to ignore the last character of the line.
Are you sure you even need this first loop? If I have understood correctly, the ScannerĀ“s next() will give you strings between the spaces; in the case of two consecutive spaces I think it should return you an empty string. In this case you could just loop the list the way you do in the end of your function, and print a space character when you encounter an empty string in your list. Otherwise just print the word backwards, just like you already do (except that it should be m-- instead of m++ in the last for loop).
But if the Scanner won't give you the empty strings when there are two or more consecutive space characters, I bet the string's split() method should work.
What am I supposed to do in order to print only the duplicated words in a file. I'm a beginner in C.
Here's my code:
#include <stdio.h>
#define max 100
main(){
FILE *fpointer;
char ch[max],str[max][max];
int i = 0,j = 0,count = 0;
int a,b;
fpointer = fopen("1.txt","r");
if(fpointer == NULL){
printf("Error!");
return 0;
}
while(fscanf(fpointer,"%s",ch)!=EOF){
strcat(str[i++],ch);
strcat(str[i++]," ");
count++;
}
for(a=0;a<count*2;a=a+2){
for(b=0;b<count*2;b=b+2){
if(strcmp(str[a],str[b])==0){
printf("%s\n",str[b]);
}
}
}
fclose(fpointer);
return 0;
}
An example of input is "shine little star shine bright in the dark star"
and the output of this program is "shine shine little star star shine shine bright in the dark star star". Please help me to print
only duplicate words. Tnx
In your inner loop, instead of starting at the beginning of the list, start one element after the current one. That way you're not comparing an element against itself or comparing any two elements twice.
for(a=0;a<count*2;a=a+2){
for(b=a+2;b<count*2;b=b+2){ // b=a+2 instead of b=0
if(strcmp(str[a],str[b])==0){
printf("%s\n",str[b]);
}
}
}
Output:
shine
star
Since you wrote duplicate words, i am guessing the words in the file will be repeated twice only.
So you second nested for loop is starting from the beginning every time, and thats where the problem is. You have to make sure that the word already checked is not iterated over again. For that you can change your second for loop a little
for(a=0;a<count*2;a=a+2){
for(b=a+2;b<count*2;b=b+2){ //notice b=a+2
if(strcmp(str[a],str[b])==0){
printf("%s\n",str[b]);
}
}
}
Note
For cases when the words are repeating more then twice, my suggestion would be to use some hashing technique to store if a word has already occurred or not. And then compare and print accordingly.
You have a couple of problems here.
Go through and write some comments. For example, put these where they belong:
// Open the file
// Read in the strings
// Compare the strings
If you do this you will notice you have some problems. For instance, what is this line for?
strcat(str[i++]," ");
It's not reading in a string. So what is it doing?
In your final loop, you want to compare each string with all the others. So obviously, you start with the first string, and you compare it to all the others:
for (a=1; a<count; a++)
if(strcmp(str[0],str[a])==0)
{
printf("%s\n",str[0]);
break;
}
Now we can compare the first string with each following string and print it out if it is duplicated. The "break" is there so we only print it once.
But this only compares the first string with the others. How do we compare all of them? Another loop, of course.
// Compare each string against all the ones after it
for (target=0; target<count; target++)
for (a=target+1; a<count; a++)
// If the strings match AND they are not both null
// (why print out copies of null strings? - nobody wants that)
if(strcmp(str[target],str[a])==0) && strlen(str[a])
{
// Print it
printf("%s\n",str[target]);
// Why is this here?
strcpy(str[a],"");
for (copies=a+1; copies<count; copies++)
if(strcmp(str[copies],str[target])==0)
strcpy(str[copies],"");
// We got a match, stop checking for this string
break;
}
See how we've got one big loop that steps through each string, and a smaller loop that steps through all the remaining strings (no point in stepping through the ones we already looked at).
Now a question for you: why is "strcpy(str[a],"") and the next for loop in there? What purpose do they serve?
This is one way to do it. Please enable warnings - you left out a library header. I've also checked array limits. You need to find a better way to limit the string input too, than just guess a large array size.
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // included header
#define MAX 100
int main(void){ // correct function
FILE *fpointer;
int reported [MAX];
char ch[MAX], str[MAX][MAX];
int index = 0, a;
fpointer = fopen("1.txt","r");
if(fpointer == NULL){
printf("Error!");
return 0;
}
while(fscanf(fpointer,"%s",ch) == 1){ // testing for 1 item read
for(a=0; a<index; a++){
if(strcmp(ch, str[a])==0){
if (reported[a] == 0) // don't report twice
printf("%s ",ch);
reported[a]++;
break;
}
}
if(a >= index) { // check if loop completed
if (index >= MAX) { // check if array is full
printf("\nToo many words read\n");
exit(1);
}
strcpy(str[index], ch); // word was not found
reported[index] = 0;
index++; // keep tabs on array length
}
}
printf("\n",ch);
fclose(fpointer);
return 0;
}
Program output:
shine star
One issue is that you are storing every word, regardless of whether it has already been seen:
while(fscanf(fpointer,"%s",ch)!=EOF){
strcat(str[i++],ch);
strcat(str[i++]," "); // ??? what is this for
count++;
}
Ideally, you'd only like to store each word once, and maintain a count of how often that word appears in the file:
while ( fscanf( fpointer, "%s", ch ) != EOF ) // whitespace is your friend, use it
{
if ( ch_is_not_already_in_str )
{
strcpy( str[count++], ch );
}
else
{
update_count_for_that_string;
}
}
Then at the end, you just loop through ch and print each string that has a count greater than 1.
for ( i = 0; i < count; i++ )
{
if ( str[i]_occurs_more_than_once )
printf( "%s ", str[i] );
}
The bits you need to figure out are ch_is_not_already_in_str, update_count_for_that_string, and str[i]_occurs_more_than_once. How would you go about searching your str array for an occurence of the string in ch? How would you maintain a counter for each string?
I tried creating an array of ten random strings that would print directions randomly. Such as first time "up down right ... rot_x" and second time "forward rot_y up ... down" etc. I tried using a char* pc and allocating memory for it with memset but that didn't work so I tried the following code but I'm getting weird output. How can I fix this?
int main()
{
int r_num;
char r_arr[10][10];
for (int i = 0; i < 10; i++)
{
r_num = rand() % 10;
switch(r_num)
{
case 0: strcpy(r_arr[0], "up");
break;
case 1: strcpy(r_arr[1], "down");
break;
case 2: strcpy(r_arr[2], "left");
break;
case 3: strcpy(r_arr[3], "right");
break;
case 4: strcpy(r_arr[4], "rot_x");
break;
case 5: strcpy(r_arr[5], "rot_y");
break;
case 6: strcpy(r_arr[6], "rot_z");
break;
case 7: strcpy(r_arr[7], "forward");
break;
case 8: strcpy(r_arr[8], "back");
break;
case 9: strcpy(r_arr[9], "reset");
break;
default:
fprintf(stderr, "Cannot process input/n");
}
}
for (int i = 0; i < 10; i++)
{
printf("%s ", r_arr[i]);
}
return 0;
}
here's my output:
up ?V? left right rot_x ?V? forward back reset
A few problems with your code are:
You aren't seeding rand(), so every run of your program will generate identical output. You need to use srand() first with a seed. Traditionally one uses time().
Secondly despite the randomness you are unrandomly (is that a word?) filling r_arr. "up" will always be first, "down" will always be second etc.... Instead you should do something like
for (int = 0; i< 10; i++) {
r_num = rand() % 10;
strcpy(r_arr[i], getDirection(r_num));
}
where getDirection() will return a string based on an integer input (e.g.: via a case statement that associates 0 with "up").
Your r_arr needs to be initialized. There is no guarantee in your current code that each entry in the array will be populated with chars before being accessed. If you implement suggestion 2 then you wont have a problem. Today however your code is accessing potentially uninitialized memory.
As noted by others above, your issue is that you're not indexing your array with the iteration number of your loop. You had:
case 0: strcpy(r_arr[0], "up");
Whereas you should have had:
case 0: strcpy(r_arr[i], "up");
The additional thing that I wanted to point out is that rand() uses a linear equation (at least on many systems) so it will be impossible for you to ever get two even numbers in a row or two odd numbers in a row, which is not very random. Hence I suggest something like:
r_num = (rand() >> 8) % 10;
As the commenters pointed out, you are randomizing not what value you put in each position but which positions get filled with their preset value. Also, your use of a switch statement here is just odd. Try something like:
char value_arr[10][10]={"up", "down", "left", "right", "rot_x", "rot_y", "rot_z", "forward", "back", "reset"}
for (int i = 0; i < 10; i++)
{
r_num = rand() % 10;
strcpy(r_arr[i], value_arr[r_num]);
}
Print the strings inside the switch instead of the for-loop at the end.
Maybe you'll also need something like:
srand (time(NULL));
here is a code that fits exactly to your need :
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// we use this instruction to get a random result each time we run the program
srand(time(NULL));
int r_num;
char r_arr[10][10];
// the dictionary array will be used to take from it the possible strings
char dictionary[10][10]={"up","down","left","right","rot_x","rot_x","rot_x","forward","back","reset"};
int i=0;
for(i=0; i<10; i++)
{
// r_num will be assigned randomly an index from dictionary tab
// the general equation is (rand()%(max-min+1))+min to get a random value
// between max and min inclusive
r_num=(rand()%(9-0+1))+0;
// we will put the random string chosen in the array r_num each time
strcpy(r_arr[i],dictionary[r_num]);
}
// this loop will print the result
for(i=0; i<10; i++)
{
printf("r_arr[%d]=%s \n",i,r_arr[i]);
}
return 0;
}
by looking at your output i noticed some strange values like ?v?,
the problem is that not all numbers between 0 and 9 will be generated by
the rand() function which mean that the corresponding array element(to those numbers) will not be initialized and therefor it contain garbage values from what ever was stored in that memory address.
i hope that explain why you are getting those strange values.
I'm doing a last assessment for the semester and interestingly enough, the code I have written seems to be devoid of errors other than a few simple ones I have just ironed out. However, I am stuck with one last error I cannot get my head around.
The program I am doing is a random numbers generator, using a while loop to generate the numbers and store them in the array, however, a second while loop has to be used to check to see if that number is already in the array, if that number is already in the array, this number has to be discarded and another value has to be obtained to put into the same index. After this the array is printed as a grid of 5x 10. However, upon using a continue command towards the end of the first loop, it comes up with the error:
Random50.java:52: error: continue outside of loop
continue;
^
As much as it seems obvious, I have no idea how to alter my code to make the program run, I was using the the continue command to return to the start of the first loop without incrementing a counter variable, so another value could be stored in the same index again.
import java.util.Random;
import java.util.Arrays;
public class Random50
{
public static void main(String[] args)
{
// Declare and initalise array
int[] random50 = new int[5];
// Declare and initalise counter variable
int i = 0;
// Declare and initalise repeater variable
int r = 0;
// Generator while loop
while (i < random50.length)
{
// Generate random number
int n = (int) (Math.random() * 999) + 1;
// Initalise variables for second while loop
int searchValue = i;
int position = 0;
boolean found = false;
// Duplicate while loop
while (position < random50.length && !found)
{
if (random50[position] == searchValue)
{
found = true;
}
else
{
position++;
}
}
// Return to first loop, determine if duplicate to return to the start of the loop early
if (found);
{
continue;
}
// Store value into array
random50[i] = n;
// Print value and add to counter variable
System.out.print(random50[i] + " ");
r++;
// reset counter variable to maintain grid
if (r == 5)
{
System.out.println("");
r = 0;
}
i++;
}
}
}
So, how can I get the continue to work or in other words, return to the start of the first loop mid loop?
The problem is your while() loop terminating instantly due to an obsolete ; probably placed by accident:
while (i < random50.length);
So your whole loop body will execute exactly once, no matter the condition (which will most likely be optimized out).
Once this is fixed, your use of continue; should work as expected.
Edit:
Same problem further down below:
if (found);
Due to this line you'll always execute the continue; following within these brackets, so the code below becomes unreachable.