Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    Greygoose is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default [Coding Help] Ascii reader String Length Comparison

    So ive begun learning Java due to the fact that its mandatory for the University and ill need to learn it sooner or later to programm trading robots ;)

    Ive written a bit of the code myself but im kind of stuck so here is the issue:

    You need to programm an ASCII reader, really an elementary programm using scanner and lines such as hasNext etc. There needs to be an error checker in there making sure the length of the input lines are all the same and the output needs to be the length and height of the Ascii Image.

    Now ive written a sample code (which actually worked to my surprise) which would check basically each line for a FIXED length and width and just close the program and display an error message if it fails to work:

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader{
    	public static void main(String args[]){
    		int countLines = 0;
    		Scanner sc = new Scanner(System.in);
    		
    		while(sc.hasNext()){
    			if(sc.next().length() != 50){
    				System.out.println("Length less than 50");
    				return;
    			}
    			
    			countLines++;
    		}
    		
    		if(countLines != 44){
    			System.out.println("Height less than 44");
    			return;
    		}
    		
    		System.out.println("OK");
    	}
    }
    The issue im having right now is that the ascii images that need to be checked may have a variable length and width, but the length (each row) needs to be the same length i.e:

    XXXXX
    XXXXX
    XXXXX
    XXXXX

    Would be valid all have the length of 5
    and this:

    XXXXX
    XXXXX
    XXXX <--- only 4 characters and not like the others that have 5
    XXXXX

    Would be invalid.

    Basically: The programm needs to read the Ascii image entered via the console, read it line by line and make sure the lines are the same length, if not abort the program with a text saying "bad imput" or something like that. If the Ascii image is valid it needs to simply display the numeric value of the length of the row and the amount of rows (height).

    I am really kind of stuck, my deadline is till the 1st of April. Ive been talking with some people about this and right now ive been recommended to use some funktions ive never heard before or have no idea how to use since my java is really quite rudamentary.

    So this is what ive come up with so far and it sort of really isnt that great:

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader{
    	public static void main(String args[]){
    		int countLines = 0;
                    int lineLength = 0
                    String imgLine = "";
    
    		Scanner sc = new Scanner(System.in);
    		
    		while(sc.hasNext()){ 
                     
    	         countLines++; //increment integer for every line read
    
                     String imgLine = sc.next()
                     int lineLength = imgLine.length() // integer lineLength gets the value of the lines length that has been read into the string
    
    if((sc.next.length() != int.lineLength) 
    {
    				System.out.println("Lines differ in Sizes Programm Terminated");
    				return;
    			}
    
    else {
    system.println(countLines + lineLength)
    
                
    }

    I know my coding is horrible and i apologize for not doing a better job.
    I just have no idea how to make sure it checks that each line has the same length =/ Also i know my Syntax is bad.. im still working on this.

    I know none of you are codemonkeys neither is it your job, but i have put some effort into this and my deadline is tight, so if any one of you could help me out and explain how this is done in the easiest way this would be greatly appreciated. I know the coding ive done here is probably useless to you but i did not want to show up emptyhanded looking like a beggar whos asking other people to do the job for him. Besides there are going to be 3 exams and this will be on the first exam, so i need to understand it aswell.

    Your help would be greatly appreciated.

    Kind regards,
    A.


    EDIT:

    The ASCII imput examples look like this:
    Java Code:
    In 	
    
    ======================.;+I.:======================
    ====================;,.###,.;=====================
    ====================,=;####..:====================
    ===================;.=##W##Y+.;===================
    ===================.:t##MM##X..===================
    ==================:.###MMM##MM.;==================
    =================;,;##WWMM####.,;=================
    ================;..###WWWWWM##M,,;================
    ================,:;##WMWMWWWW##..,================
    ================.XW#WWWWWWBWWW#WW.;===============
    ===============:.B##WW#WWWWWMMW##..;==============
    ===============.Y##WWWMWWMWWW#M###.:==============
    ==============:.###WWWWWMWWWMMMW##.,:=============
    =============:,.##WWMMWMWWMWWWBMW#i=.=============
    =============.t##WWW#######WW#WMM##V.:============
    ============:.=##WW############MMB#Bt.============
    ============.IX#MW##Y=.iiiitVB###W###.;===========
    ============.W##M##W....;YIii+t######.,;==========
    ===========:.####MV,...BtiYYYI=YY#..t#=:==========
    ==========;.Y##Y#BRIXRVIYVIIIIiYI#...#..;=========
    ==========:.#i..MXB+IIIXXXVRXX+XR#...#=:,=========
    =========:,.#...X##,+IRMRXXXXY,###...t#Y..;=======
    ========,.;R#....M##VIX+IYIIYR###.....###..,;=====
    ======;..W##.....,i####RRR#####:,..,..XM##X;.,====
    ====;...+###.......,IB#####MVi....,,,.=I##XV#..,==
    =;,..=####MV..,,......;;it;=.....,,,,..+#W;.V##..:
    :.:X##..##I..,,..................,,,,,..#+....Y#:.
    .=##t...i#t..,,.,..........,,,,,,.,..:.,Y#=.=:..#Y
    :#t..::.i#i........,,..,,,.:,,,,,,,,,,...#+.=,..#i
    .##+.::,#t:....,...,,,,.,,.:,.,,,,..,,,,.#;...:##;
    .i##R...#....,..,..,,,.,,,.....,,,.,,,,,.#=..###..
    ,.W##..,#.........,...,,,,,,,,,...,,.,,:.V####X..=
    =,.i#####...,,,.,...,,,,,,.,,,:,,,,,,,,,.,M##MY.;=
    =;.;I###i..,,,...,,,..,.,,,,..,..,.,,.,,..B##MY.;=
    ;,.:Y###...,,,.,....,,.,..,.,,.,,.,,,,,,..X###W.,=
    ,:iB####...,,,.,.,,...,.,,,,,,....:,,,,,:.IB##MY.;
    ,####W##...,,,...........,,,,,,,,,.,,.,,,.tR####.;
    :#######...,,..,.................:.,:,,,:.tR####.+
    ..X##t##...,.........;=VXXR=......:,.,,,:.Y###Wi.;
    =:....##........;X#############t,...,,,,..M#....:=
    =====.##M.....i###Vi.........+V##W...,,,..##.:;===
    =====.=i#...####...,:======;:,..t##t.....BXB.=====
    =====;,,#####:..,;============;:..B##...,#i.:=====
    ======;,......;==================:..t####+,:======
    And in this case the Required output must be: 50 44 because the length is 44 and the width is 50. Problem is all the asciss they will check have different lengths and widths and as i said in here repeatedly i have no idea how to implement this for variable lengths and widths.
    Last edited by Greygoose; 03-29-2011 at 12:07 AM.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Your String index logic could be wrong. Well let's say you got the String "Hello". "Hello".length() is 6. It may have 5 characters, but indexes in Java start at 0, and starting from left of the H and ending at the right of the O, the String has 6 indexes.

    Let me show you:

    index 0 H index 1 E index 2 L index 3 L index 4 O index 5

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    int.lineLength
    Does that look like valid syntax to you?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Also, you have declared the lineLength variable twice in your code.

    And another thing, ask a specific question. Do not just dump your homework assignment and a bunch of code and expect us to figure out what is wrong. Provide us with some vital information. Does it compile? No then copy and paste the exact error message. Does it run but give incorrect output? Yes then provide us examples of expected and actual output.

  5. #5
    Greygoose is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    No obviously not at that part its really just becoming sort of pseudocode-ish. Look im really trying to learn here fact is so far ive spent about 5 hours on this problem and wasnt able to solve it. Tried googleing the issue etc and found nothing so yeah. Should have put // This is stupid pseudocode because im stuck but want to display my thought process on how i am trying to tackle the issue, so that the coding gods under you could help put me on the right track or explain to me how to solve this problem and WHY my approach to this problem is bad so that i will not repeat these stupid mistakes in the future ;)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    OK

    I still have no idea what your problem is. As I said you need to ask a specific question. "How do I do this?" is not specific.

  7. #7
    Greygoose is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Also, you have declared the lineLength variable twice in your code.

    And another thing, ask a specific question. Do not just dump your homework assignment and a bunch of code and expect us to figure out what is wrong. Provide us with some vital information. Does it compile? No then copy and paste the exact error message. Does it run but give incorrect output? Yes then provide us examples of expected and actual output.
    I'm not dumping my homework as i was busy trying to find out how i would solve this problem for FIXED width and length ascii. Ive been trying to fool around with solutions but i am not able to find the correct approach.

    Im not asking you to solve this for me, if you do that its fine with me, but im screwed either way unless i understand why my approaches and thought processes are wrong.

    If you want a specific question here it is:

    What is the best approach to tackle this problem, and im talking about specifically the problem of comparing each lines length while being in the "while (sc.hasnext()) loop". Because im not understanding how exactly im supposed to use the string to find the length AND compare each following line.

  8. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    It would look something like this (I had it store the checked lines in an array as well):

    Java Code:
    public static final int length = 50;
    public static final int width = 44;
    String[] lines = new String[44];
    String line;
    Scanner in = new Scanner(System.in);
    
    for(int i=0; i<width; i++) // must enter 44 lines, your width (if I'm correct in reading)
    {
    System.out.println("Enter a line. You have "+44-i+" lines remaining.");
    line=in.nextLine();
    if(line.length() != length) // if your line doesn't have 50 indexes (your length)
    {
    System.out.println("Your input did not contain 50 indexes! Exiting in 5 seconds...");
    System.exit(5000);
    } else {
    lines[i] = line;
    }
    }
    Instead of using while loops and having to do all this extra work for counting, use for loops.
    Last edited by Solarsonic; 03-29-2011 at 12:44 AM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I know I'm being a bit of a harda$$ but I am trying to get you to think about the problem and improve your communication skills at the same time.

    You still ahven't asked a specific question, rather just reworded "How do I do this?". But moving on.

    I would have a variable initialised to -1 (since Strings can only have a length equal or greater than 0). Read a line. If variable is -1 this must be the first line so store the length of the line in the variable. Else compare length of the line to variable. If they are not the same then display error message and terminate program. Otherwise increase counter. When loop is done display statistics.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    @solarsonic

    Do not hard code values. Programs should work regardless of input.

  11. #11
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    @solarsonic

    Do not hard code values. Programs should work regardless of input.
    I hard coded it because his program specifically needed those values... good code I would imagine?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    No it is cr@p code. As I said a program should work regardless of the input. Do you really think that when it comes to the teacher marking the assignment that the Duke ASCII picture seen above is the only test the teacher will use? Yeah sure it might pass that test but it will fail every other test the teacher uses. Hey but if you are fine writing code that only performs to a strict set of parameters and is totally useless otherwise then go right ahead. Just don't get a job where I work!

  13. #13
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    No it is cr@p code. As I said a program should work regardless of the input. Do you really think that when it comes to the teacher marking the assignment that the Duke ASCII picture seen above is the only test the teacher will use? Yeah sure it might pass that test but it will fail every other test the teacher uses. Hey but if you are fine writing code that only performs to a strict set of parameters and is totally useless otherwise then go right ahead. Just don't get a job where I work!
    You really need to chill out. I don't see how me using that in an example is a big deal. Anyone with even complete basic knowledge would know how to change the final value or obtain it from input.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Then stop spoonfeeding. You keep saying that you won't provide code but the very next thread there you go again serving up code and cr@p code at that.

  15. #15
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Then stop spoonfeeding. You keep saying that you won't provide code but the very next thread there you go again serving up code and cr@p code at that.
    I personally believe that a little general spoonfeeding to push something who is clueless in the right direction is worth it. It is obvious that he has put effort into this.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Maybe a little spoonfeeding might help but you provide fully coded solutions. This is not "little". Also, you have some bad coding practices as I assume you are still learning yourself. I would prefer it if you did not pass these bad practices onto other people.

  17. #17
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Maybe a little spoonfeeding might help but you provide fully coded solutions. This is not "little". Also, you have some bad coding practices as I assume you are still learning yourself. I would prefer it if you did not pass these bad practices onto other people.
    Sure, I am still learning myself, I consider myself at the intermediate level. For this reason I like to code the fully coded solutions to better my Java through practice. I suppose I can leave that outside of my posts.

    About the bad practices, feel free to tell me if I use it. I'm sorry for flipping out on you, its just that it seems like you're always bitching at everyone so I didn't take you seriously.

  18. #18
    Greygoose is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    I hard coded it because his program specifically needed those values... good code I would imagine?

    Thanks mate, but if the values would need to be specific or hardcoded, check the first programm i wrote at the top, it would do the Job just much simpler.

    Im saying this because i didn't get to read the chapter about arrays yet but i think im understanding the basic concept of it.

    @Junky: I'm rather thick skinned so i don't mind the tone ;)

    Im really exhausted from trading all day so ill need a couple of hours of sleep, ive tried rewriting this:

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader{
    	public static void main(String args[]){
    		int imgLength, countLines = 0;
              String imgLine = "";
    
    		Scanner sc = new Scanner(System.in);
    		
    		while(sc.hasNext()){ 
                     
    	         countLines++; //increment integer for every line read
    
                     String imgLine = sc.next() //dumping the first line into the String imgLine
    			  
    				int imgLength = imgLine.length() // int imgLength is given the numerical value of imgLine String
    
    		if((sc.next.length() != imgLength) { // Would This start comparing each of the next lines to the value stored in imglength?
    
    				System.out.println("Bad ASCII Image, Line length differs, Please recheck input");
    				return;
    
    									}
    
    		else {
    				system.println(countLines + imgLength)
    
    }
    Is this beginning to make more sense and moving into the right direction? Because what im afraid of (and this is because im a total newbie) is that the loop keeps overwriting the imgLength variable, also im not sure if that if command is going to work.

    =/

    Kind regards,
    Alexander

  19. #19
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Greygoose View Post
    Thanks mate, but if the values would need to be specific or hardcoded, check the first programm i wrote at the top, it would do the Job just much simpler.

    Im saying this because i didn't get to read the chapter about arrays yet but i think im understanding the basic concept of it.

    @Junky: I'm rather thick skinned so i don't mind the tone ;)

    Im really exhausted from trading all day so ill need a couple of hours of sleep, ive tried rewriting this:

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader{
    	public static void main(String args[]){
    		int imgLength, countLines = 0;
              String imgLine = "";
    
    		Scanner sc = new Scanner(System.in);
    		
    		while(sc.hasNext()){ 
                     
    	         countLines++; //increment integer for every line read
    
                     String imgLine = sc.next() //dumping the first line into the String imgLine
    			  
    				int imgLength = imgLine.length() // int imgLength is given the numerical value of imgLine String
    
    		if((sc.next.length() != imgLength) { // Would This start comparing each of the next lines to the value stored in imglength?
    
    				System.out.println("Bad ASCII Image, Line length differs, Please recheck input");
    				return;
    
    									}
    
    		else {
    				system.println(countLines + imgLength)
    
    }
    Is this beginning to make more sense and moving into the right direction? Because what im afraid of (and this is because im a total newbie) is that the loop keeps overwriting the imgLength variable, also im not sure if that if command is going to work.

    =/

    Kind regards,
    Alexander
    Yes, this is beginning to make more sense but its still poorly written and using while loops when for loops could be used.

    Take a look at my code and try using my way of doing it.

  20. #20
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    Yes, this is beginning to make more sense but its still poorly written and using while loops when for loops could be used.

    Take a look at my code and try using my way of doing it.
    what's wrong with while loops?

Page 1 of 2 12 LastLast

Similar Threads

  1. String comparison based on system locale
    By couling in forum Advanced Java
    Replies: 2
    Last Post: 02-14-2011, 09:03 PM
  2. String object comparison issue
    By subith86 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 06:18 AM
  3. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 08:11 AM
  4. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 12:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •