Results 1 to 20 of 32
- 03-29-2011, 12:02 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
[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:
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: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"); } }
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:
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.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####+,:======
Last edited by Greygoose; 03-29-2011 at 12:07 AM.
- 03-29-2011, 12:12 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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
- 03-29-2011, 12:16 AM #3
Does that look like valid syntax to you?Java Code:int.lineLength
- 03-29-2011, 12:19 AM #4
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.
- 03-29-2011, 12:20 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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 ;)
- 03-29-2011, 12:25 AM #6
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.
- 03-29-2011, 12:27 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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.
- 03-29-2011, 12:35 AM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
It would look something like this (I had it store the checked lines in an array as well):
Instead of using while loops and having to do all this extra work for counting, use for loops.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; } }Last edited by Solarsonic; 03-29-2011 at 12:44 AM.
- 03-29-2011, 12:39 AM #9
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.
- 03-29-2011, 12:40 AM #10
@solarsonic
Do not hard code values. Programs should work regardless of input.
- 03-29-2011, 12:43 AM #11
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-29-2011, 12:50 AM #12
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!
- 03-29-2011, 12:53 AM #13
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-29-2011, 12:55 AM #14
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.
- 03-29-2011, 12:58 AM #15
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-29-2011, 01:08 AM #16
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.
- 03-29-2011, 01:15 AM #17
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
- 03-29-2011, 01:22 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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:
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.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) }
=/
Kind regards,
Alexander
- 03-29-2011, 01:46 AM #19
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-29-2011, 02:49 AM #20
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Similar Threads
-
String comparison based on system locale
By couling in forum Advanced JavaReplies: 2Last Post: 02-14-2011, 09:03 PM -
String object comparison issue
By subith86 in forum New To JavaReplies: 3Last Post: 01-19-2011, 06:18 AM -
String Comparison
By evant8950 in forum Java AppletsReplies: 6Last Post: 04-22-2009, 08:11 AM -
String comparison
By abhiN in forum New To JavaReplies: 2Last Post: 04-09-2008, 04:47 AM -
String comparison
By sireesha in forum New To JavaReplies: 1Last Post: 12-18-2007, 12:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks