Results 1 to 12 of 12

Thread: java program.     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    java program.

    Ok what I have to do is read a file. The file will contain a 5 by 8 list of floats:

    example:

    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6

    Once i read read in the integers i need to put them in a if statement. Saying that if the number is less than five it is presented as a 0. If its greater than 5 its represented as a 1. I then need to print out the presentation of the numbers in a 2d Array.

    The only problem im really having is how to put them into a 2d array after i've converted them into a 0 or 1. Can anyone point me in the right direction please.


    This is my code so far. It doesnt put into an array just ouputs the new numbers back into the file. or so I hope lol am I on the right track?
    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Doom {
        public static void main(int[] args) {
            try {
                Scanner scanner = new Scanner(new File("input.txt"));
                PrintWriter out = new PrintWriter("output.txt");
                while(scanner.hasNext()) {
                    int s = scanner.next();
    				if (s > 5) {
    					s = 1;
    				} 
    				else {
    					s = 0;
    				}
                    out.println(s);
                }
                scanner.close();
                out.close();
            }
            catch(FileNotFoundException e) {}
        }
    }

    2 errors atm:

    .java:15:15 class Doom is public, should be declared in file named Doom public class Doom {

    .java:21: incompatible types found: Java.lang.String
    required int int s= scanner.next();

  2. #2
    Hydra
    Join Date
    Dec 2006
    Posts
    125
    BG Level
    3

    not great with programming, but as for the 2nd error, maybe try double s = scanner.nextFloat(); ? Then convert the double into a new int variable and subtract this new int from s to leave you with the decimal and use that for your if statements

  3. #3
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Balthasar View Post
    not great with programming, but as for the 2nd error, maybe try double s = scanner.nextFloat(); ?
    Fixed second error, thanks! also i figured out first error on my own. now only question is how do i put the digits into a 2d array.

    Code:
    int[][] x = new int[5][8];
    creates the array

    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ArrayAverage {
        public static void main(int[] agrs) {
            try {
                Scanner scanner = new Scanner(new File("input.txt"));
                PrintWriter out = new PrintWriter("output.txt");
                while(scanner.hasNext()) {
                    double s = scanner.nextFloat();
    				if (s > 5) {
    					s = 1;
    				} 
    				else {
    					s = 0;
    				}
    				int[][] x = new int[5][8];
    				for(int i = 0; i < x.length; i++)
    					for(int j = 0; j < x[i].length; j++)
    						x[i][j] = s;
                    out.println(s);
                }
                scanner.close();
                out.close();
            }
            catch(FileNotFoundException e) {}
        }
    }
    On the right track?

    and now its complaining about the double in on the line saying x[i][j] = s;

    .java:30: possible loss of precision found : double required int x[i][j] = s;

  4. #4
    Burninate all the things.
    Join Date
    Apr 2008
    Posts
    3,356
    BG Level
    7

    I think a lot of your troubles are originating from the fact that ints can only be whole numbers. If the guidelines of your assignment, as this is obviously homework, require you to use int and only int with that input, you're going to have to cast the numbers into ints. Which, if I remember correctly, would cause them to lose their decimal points so I doubt they're asking you to do that.

    Your array is giving you trouble because you're trying to shove doubles into an int array. doubles can hold decimal points, ints cannot. Thus your loss of precision, Java is basically saying "If you're going to force this square peg into this round hole with Thor's Hammer, you'll end up shaving off the corners."

    Short answer, use doubles throughout and you will be a lot better off.

    Edit: Upon further examination of you're code, I'm not sure it's doing what you want it to be doing. For instance, you're declaring a new array for every value of s, and then you're filling every single value of that array with either 0 or 1. Then, you're finishing it off by printing out 0 or 1?

    Your syntax aside from the double/int deal looks fine but your logic is pretty iffy. I'd take a step back and evaluate it one step at a time. If you want one, persistent Array that you can print out at the end of runtime, then declare the array in the beginning. Declaring it in the middle of a while loop will only cause you to constantly create new ones for every value it reads from the file, and overwrite the old ones (Not just because it's the same name, but because memory is only being allocated for it in a temporary setting while in that loop). If you want to add each new value of s to the Array once, then only add it once. Don't set up for loops to set every single value in the array equal to any given value of s.

    You're making the assignment a lot more complicated than it really is. Try a different approach and see what your classmates are doing. This looks like intro to Java material so I'm sure there are study groups abound.

  5. #5
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    The output file you generate via your code is going to be a single column since you're using the println method, use print and delimit with the \t or space or whatever the original file has for consistencies sake.

    As for your array issues, inputting values into an array is very easy:

    Code:
    0,0 0,1 0,2
    1,0 1,1 1,2
    2,0 2,1 2,2
    These are the values you'd use for the accessing the specific element of the array. So to assign the first value you'd use: array[0][0]=value;

    With this in mind, if you do want to construct that 2D array you'll need to do a know the integer values to insert the 0|1 at, so think about how you could map the reading to array areas. If you are allowed to modify your input file then just include the height and width of the array in the first line and use those two values to create and then assign values to your array.

  6. #6
    Hydra
    Join Date
    Jul 2008
    Posts
    122
    BG Level
    3

    Quote Originally Posted by Skie View Post
    Fixed second error, thanks! also i figured out first error on my own. now only question is how do i put the digits into a 2d array.

    Code:
    int[][] x = new int[5][8];
    creates the array

    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ArrayAverage {
        public static void main(int[] agrs) {
            try {
                Scanner scanner = new Scanner(new File("input.txt"));
                PrintWriter out = new PrintWriter("output.txt");
                while(scanner.hasNext()) {
                    double s = scanner.nextFloat();
    				if (s > 5) {
    					s = 1;
    				} 
    				else {
    					s = 0;
    				}
    				int[][] x = new int[5][8];
    				for(int i = 0; i < x.length; i++)
    					for(int j = 0; j < x[i].length; j++)
    						x[i][j] = s;
                    out.println(s);
                }
                scanner.close();
                out.close();
            }
            catch(FileNotFoundException e) {}
        }
    }
    On the right track?

    and now its complaining about the double in on the line saying x[i][j] = s;

    .java:30: possible loss of precision found : double required int x[i][j] = s;
    You get that error because s is of type double and you are trying to store it as an int.
    Either downcast s to an int before storing it or create a new variable of type int to store the 1 or 0.

    Also your for loop is just going to fill the entire array with a 0 or a 1 everytime you read in a number.

  7. #7
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Thanks for all the advice i'll work on it and see if i fix what was suggested.

    This is what i have now

    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ArrayAverage {
        public static void main(int[] agrs) {
            try {
                Scanner scanner = new Scanner(new File("input.txt"));
                PrintWriter out = new PrintWriter("output.txt");
    			double[][] x = new double[5][8];
                while(scanner.hasNext()) {
                    double s = scanner.nextFloat();
    				if (s > 5) {
    					s = 1;
    				} 
    				else {
    					s = 0;
    				}
    				x[0][0] = s;
    			out.println(s);
                }
                scanner.close();
                out.close();
            }
            catch(FileNotFoundException e) {}
        }
    }
    Was posted to get rid of the for loop and to move the array outside the while. did X[0][0] = s; but that would only handle the first value of the array would it not. I have to put it in a for loop to add for each value wouldent i?

    Appricate the help thanks guys

  8. #8
    Hydra
    Join Date
    Dec 2006
    Posts
    125
    BG Level
    3

    Quote Originally Posted by Greatguardian View Post
    and see what your classmates are doing.
    but for godsakes don't use the same variable names as them b/c that's the #1 way to get accused of plagiarism

  9. #9
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Due at midnight and dont really talk to my classmates that much :/

  10. #10
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I did this with the loop after thinking about it a little more and reading post

    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ArrayAverage {
        public static void main(int[] agrs) {
            try {
                Scanner scanner = new Scanner(new File("input.txt"));
                PrintWriter out = new PrintWriter("output.txt");
    			int y;
    			double[][] x = new double[5][8];
                while(scanner.hasNext()) {
                    double s = scanner.nextFloat();
    				if (s > 5) {
    					y = 1;
    				} 
    				else {
    					y = 0;
    				}
    				for(int i = 0; i < x.length; i++)
    					for(int j = 0; j < x[i].length; j++)
    						x[i][j] = y;
    			out.println(s);
                }
                scanner.close();
                out.close();
            }
            catch(FileNotFoundException e) {}
        }
    }
    Does this work? I cant run the programs on this computer it keeys saying its not recongnized as an internal or external command

  11. #11
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    Download the JDK. In command line javac filename, java filename. Alternatively I recommend an IDE like eclipse, it will auto compile as you type in it's editor and help you catch errors as you make them. You need to be testing as you program because I can tell just by looking at your code it is not going to do what you expect. You need to rethink how you're parsing through this file and generating the array, they need to be mapped better. Currently you're just filling in the entire array with the value of each item that's scanned, as an above poster mentioned. This assignment is fairly simple but I find it hard to believe you've gotten to this level of programming, albeit very simple and still don't know how to compile and run a java file on any machine. Nearly every OS has java support...

  12. #12
    Hydra
    Join Date
    Jul 2008
    Posts
    122
    BG Level
    3

    1.)
    Does your text file have to contain commas like you posted:
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6
    1.1, 2.2, 3.3, 4.5, 6.6

    Or can it be without like:
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6
    1.1 2.2 3.3 4.5 6.6

    scanner.nextFloat won't work properly due to the comma.

    2.) Your array should be of type byte(int is acceptable), since you are storing either 0 or 1

    3.) The array you have has the row and column mixed up. Row comes first then column. So it should be [8][5] (8 rows by 5 column).

    4.) Depending on answer to #1, your while statement can use hasNextFloat().

    5.) You don't need a for loop in the while statement to assign the 0 or 1 to the array. Since you are going through the list one number after another, you can directly assign 0 or 1 into your array after the condition is checked. You will also have to keep track of the current position in the array.

    Example:
    int row,col = 0 // First element in the array, initial position

    while(condition){
    if(number < 5)
    result = 0;
    else
    result = 1;

    check to make sure current array position is within index bound (if not reset col, increment row)
    store result into array
    increment column so that when next number is read it can be stored into available position in array
    }

    Now you have a 8x5 array holding the 0 or 1 and can write to a file using for loop.

    Edit:
    Also you may have to change public static void main(int[] agrs) to
    public static void main(String[] args)

Similar Threads

  1. Java program Help: KI tracker
    By Skie in forum Tech
    Replies: 19
    Last Post: 2011-04-08, 09:31