Have the
return 0;
statement outside the for loop.
If you do not wish to count the words of each file seperately, you will also need to modify your code accordingly.
thanks...i knew it was something simple :)
Have the
return 0;
statement outside the for loop.
If you do not wish to count the words of each file seperately, you will also need to modify your code accordingly.
thanks...i knew it was something simple :)
i have tried that but it is still only doing it for the first file...
#include <stdio.h>
int main (int argc, char *argv[])
{
int i;
for (i = 0; i<=argc; i++)
{
int cur;
int countLn = 0;
int countCh = 0;
int countWd = 0;
FILE *fp1;
fp1 = fopen(argv[i+1], "r");
if (!(fp1))
{
printf("Error in opening", argv[i+1], "for reading");
return (1);
}
while ((cur = fgetc(fp1)) != EOF)
{
if (cur != '\n')
countCh++;
if (cur == ' ')
countWd++;
if (cur == '\n')
countLn++;
}
printf("\n");
printf("Number of characters: %d\n", countCh);
printf("Number of lines: %d\n", countLn);
printf("Number of words: %d\n", countWd);
printf("\n");
fclose(fp1);
return 0;
}
}
Hi there i am trying to make a program that takes multiple files from the command line.
I think i have got my my word count working right, but my question is how do i get it to read multiple files on the command line. Like when i do
./a.out wordcount.c file1 file2
heres my code any help would be appreciated
#include <stdio.h>
#define Space (cur == ' ' || cur == '\n' || cur == '\t')
int main (int argc, char *argv[])
{
int cur;
int preCh;
int countLn = 0;
int countCh = 0;
int countWd = 0;
FILE *fp1;
if (!(fp1 = fopen(argv[1], "r")))
{
printf("Error in opening", argv[1], "for reading");
return (1);
}
while ((cur = fgetc(fp1)) != EOF)
{
if (cur != '\n')
countCh++;
if (cur == ' ')
countWd++;
if (cur == '\n')
countLn++;
}
printf("\n");
printf("Number of characters: %d\n", countCh);
printf("Number of lines: %d\n", countLn);
printf("Number of words: %d\n", countWd);
printf("\n");
fclose(fp1);
return 0;
}
Um you really shouldnt be asking that ....
Put this line in your constructor.
System.out.println(x + " " + y + " " + ...);
And just to be sure, have you re-compiled your code after making the changes JC suggested in this first post ?
I know how to write it I'm just not sure why I am putting it in my waypoint class...it won't do anything there?
Yes, I got that. Try the print I suggested.
how would i do that print??
In that case you should try to understand what you are doing... currently you are erasing the values in the arguments you passed to your method waypoint() by the values already present in the member variables by default (in this case 0).
After the corrections JC pointed out, print out the values being read from the file returned by wps.nextInt(), maybe the problem is in the waypoints.txt file ??
--> Too late .. guess should refresh my page more often :P
if i do
Sytem.out.print(wps.nextInt()," ");
it prints out all the values in the waypoints.txt correctly. but when i pass them into my waypoint() method they all go to 0...
You need them like
x = myX;
If that still doesn't work put a load of print statements into your code, printing the values of the key variables at each stage so you cen see where it's going wrong.
I changed them to
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
public class waypoint {
int x;
int y;
int height;
int cost;
int gold;
int mapX;
int mapY;
public waypoint(int myX,int myY,int myHeight,int myCost,int myGold,int myMapX,int myMapY){
x=myX;
y=myY;
height=myHeight;
cost=myCost;
gold=myGold;
mapX=myMapX;
mapY=myMapY;
}
public int myHashKey(){
String hashable=(Integer.toString(x)+Integer.toString(y)+Integer.toString(height)+Integer.toString(cost)+Integer.toString(gold)+Integer.toString(mapX)+Integer.toString(mapY));
return hashable.hashCode();
}
}
and I made a test instance to print stuff..
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
public class test{
public static void main(String[] args) throws FileNotFoundException {
Scanner wps = new Scanner(new FileReader("waypoints.txt"));
while(wps.hasNextInt()){
if(wps.hasNextInt()){
waypoint w = new waypoint(wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt());
System.out.print(w.x+" ");
}
}
}
}
anytime i print w.x, or w.y anything from in the waypoint i get a line of 0's
but i know the scanner is working and reading them becasue i also tried
public class test{
public static void main(String[] args) throws FileNotFoundException {
Scanner wps = new Scanner(new FileReader("waypoints.txt"));
while(wps.hasNextInt()){
if(wps.hasNextInt()){
int x=wps.nextInt();
System.out.print(x+" ");
}
}
}
I get the numbers from the file, I dont know why when i try to pass them into waypoint the just get zero'ed out...
In your constructor, lines 17-23 you have all the assignments the wrong way round.
I have done it both ways but my w.x and w.y..etc still all are coming out to 0 instead of what should be passed from the file.
I am trying to create waypoints from a given file waypoints.txt
I first created the waypoint class
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
public class waypoint {
int x;
int y;
int height;
int cost;
int gold;
int mapX;
int mapY;
public waypoint(int myX,int myY,int myHeight,int myCost,int myGold,int myMapX,int myMapY){
myX=x;
myY=y;
myHeight=height;
myCost=cost;
myGold=gold;
myMapX=mapX;
myMapY=y;
}
public int myHashKey(){
String hashable=(Integer.toString(x)+Integer.toString(y)+Integer.toString(height)+Integer.toString(cost)+Integer.toString(gold)+Integer.toString(mapX)+Integer.toString(mapY));
return hashable.hashCode();
}
}
then i tried reading it and creating the waypoints using this...
public void waypoints(String[] args) throws FileNotFoundException {
Scanner wps = new Scanner(new FileReader("waypoints.txt"));
while(wps.hasNextInt()){
if(wps.hasNextInt()){
waypoint w = new waypoint(wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt(),wps.nextInt());
animatePanel.addPermanentDrawable(new Marker((int) w.x,(int) w.y, Color.black));
}
}
}
and it wasn't creating anything so i tried printing w.x to see what it was putting and i just get 0's not sure why, if you could point me in the right direction it would be much aprectiated.
Hi i have been trying to write a code that takes a picture and gets the odd valued pixels and turns them red, and also get the even values pixels and make them brighter. I am stuck and any help would be appriciated.
/**
* Decrypt.java
* Starter file for secret image Comp 110 project
* Ian Jones
*/
import java.awt.Image;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
public class Decrypt {
String path, pictureFile;
Picture aPicture;
Pixel aModified;
Pixel aBrightened;
public Decrypt() {
path = System.getProperty("user.dir");
FileChooser.setMediaPath("c:/Documents and Settings/Biff/Desktop/");
System.out.println("Get the picture to decrypt");
pictureFile = FileChooser.pickAFile();
aPicture = new Picture(pictureFile);
System.out.println(aPicture);
aPicture.show();
}
public void decryptPicture() {
Pixel[] pixel;
aPicture.explore();
for(int x=0; x<aPicture.getWidth()-1; x++){
for(int y=0; y<aPicture.getHeight()-1; y++){
if((x+y)%2==0)
pixel.setRed(255);
pixel.setBlue(0);
pixel.setGreen(0);
}
}
System.out.println("modified " + aModified + " brightened " + aBrightened
+ " pixels");
aPicture.repaint();
aPicture.write("decryptedPicture.png");
System.out.println("wrote decryptedPicture.png");
}
public static void main(String[] args) {
Decrypt app = new Decrypt();
app.decryptPicture();
System.out.println("Decrypt is done");
}
}