Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    for variable length input how would you propose he accomplishes handling the scanner input with a for loop? Not hard coded values

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

    Default

    Quote Originally Posted by yellowledbet View Post
    what's wrong with while loops?
    If you're going to be doing counting. You might as well use a for loop.

  3. #23
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    About the bad practices, feel free to tell me if I use it.
    Start by making it a habit to read the API for classes and methods you use. System.exit(5000) doesn't do what you apparently think it does.

    db

    Java Code:
    System.out.println("Your input did not contain 50 indexes! Exiting in 5 seconds...");
    System.exit(5000);

  4. #24
    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
    If you're going to be doing counting. You might as well use a for loop.
    aren't we reading from a buffer?
    Last edited by yellowledbet; 03-29-2011 at 06:19 PM.

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

    Default

    Uhh the data is being red from a buffer, my goal is to keep everything as minimal as possible, ill try compiling the code in an hour or so and finding out where my mistakes are.

    I will be working with 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)
    
    }
    Because theyre expecting us to use string to determine the length and theyre expecting us to use next and or Hasnext loops.
    So obviously im not going to create an array for that.

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

    Default

    Okay so ive written this in eclipse, havent tested it yet, what do you guys think?

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader {
    	public static void main(String args[]){
    		int imgLength = 0, countLines = 0;
    		String imgLine ="";
    		boolean dataokay = true;
    		Scanner scan = new Scanner(System.in);
    		
    		while(scan.hasNext()){
    			if (scan.hasNext()) {
    			imgLine = scan.next();
    			imgLength = imgLine.length();
    			} 
    			else {
    				dataokay= false;
    				
    			}
    		if(scan.next().length() != imgLength){
    			System.out.println("Input Mismatch");
    			return;
    		}
    		countLines++;
    		System.out.println(imgLength + countLines);
    		}
    		if (dataokay){
    			System.out.println("Input Mismatch");
    		}
    		}
    }
    I know its not as elegant as real coders like you would expect but ive tried hard and this is the best i could come up with so far.

    Any comments? Suggestions?

    Need to start testing this

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

    Default

    Okay nvm this is just forcing me to enter in lines and it will compare them... will try to recode =/

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

    Default

    you are trying to read from a file, right?

    An example from A Java FileReader class example | devdaily.com

    Java Code:
     // wrap a BufferedReader around FileReader
      BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
    
      // use the readLine method of the BufferedReader to read one line at a time.
      // the readLine method returns null when there is nothing else to read.
      while ((line = bufferedReader.readLine()) != null)
      {
        records.add(line);
      }
    Last edited by yellowledbet; 03-29-2011 at 04:06 PM.

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

    Default

    Quote Originally Posted by yellowledbet View Post
    you are trying to read from a file, right?

    An example from A Java FileReader class example | devdaily.com

    Java Code:
     // wrap a BufferedReader around FileReader
      BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
    
      // use the readLine method of the BufferedReader to read one line at a time.
      // the readLine method returns null when there is nothing else to read.
      while ((line = bufferedReader.readLine()) != null)
      {
        records.add(line);
      }

    Hmmm ill try looking into that but i want to keep it as simple as possible, i did make some progress right now though

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader2 {
    	public static void main(String args[]){
    		int imgLength = 0, countLines = 0;
    		String imgLine ="";
    		Scanner scan = new Scanner(System.in);
    		
    		while(scan.hasNext()){
    			if (scan.hasNext()) {
    			imgLine = scan.next();
    			imgLength = imgLine.length();
    			} 
    			else {
    				dataokay= false;
    				
    			}
    		if(scan.next().length() != imgLength){
    			System.out.println("Input Mismatch");
    			return;
    		}
    		
    		System.out.println(imgLength);
    		}
    		if (dataokay){
    			System.out.println("Input Mismatch");
    		}
    		}
    }
    Using this code when executing the file with java AsciiReader < Asciishop.io1 > Asciishop.out1

    im getting this:
    Java Code:
    41
    41
    41
    Input Mismatch
    Now this is great because the ascii file i entered has exactly 41 characters in length. So thats good.

    Ive also come to understand that because of the way i coded it the last Input Mismatch is because of the Boolean value if thingy i did. Im trying to remove that now.

    Question is WHY is the lingelength being printed THREE times???

    The result should be: Linelength Amountoflines

    I think coding wise ive pretty much hit a dead end... any ideas or solutions and explanations as to why my loop is printing out the linelength three times?

    How do i fix this?

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

    Default Almost Done

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader {
    	
    	public static void main(String args[]){
    		int imgLength = 0, countLines = 0;
    		String imgLine ="";
    		Scanner scan = new Scanner(System.in);
    		imgLine = scan.next();
    		imgLength = imgLine.length();
    		
    		while(scan.hasNext()){  // as long as there is input/Something to read in
    			
    			imgLine = scan.next(); // String imgLine gets fed the next line
    			countLines++; // Counter is increased by one for each line fed
    		
    		if(imgLine.length() != imgLength){ // if the read imgLine string length does not equal the imgLength integer
    			System.out.println("Input Mismatch"); // then abort by printing input mismatch and close
    			return; // abort
    		}
    		}
    		
    		System.out.print(imgLength); //post imglength
    		System.out.print(countLines); //post countlines
    		
    		} 
    		}
    Almost done, found out that i dont need to assign values within the loop i can do that before the loop (Sigh im an idiot... someone could have told me)


    A friend of mine told me this code is much better but apparently its still not working and he told me i should be able to spot the mistake... i cant find it.. any ideas?

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

    Default Finished!!

    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader {
    	
    	public static void main(String args[]){
    		int imgLength = 0, countLines = 1;
    		String imgLine ="";
    		Scanner scan = new Scanner(System.in);
    		imgLine = scan.next();
    		imgLength = imgLine.length();
    		
    		while(scan.hasNext()){  // as long as there is input/Something to read in
    			
    			imgLine = scan.next(); // String imgLine gets fed the next line
    			countLines++; // Counter is increased by one for each line fed
    		
    		if(imgLine.length() != imgLength){ // if the read imgLine string length does not equal the imgLength integer
    			System.out.println("Input Mismatch"); // then abort by printing input mismatch and close
    			return; //brich ab
    		}
    		}
    		
    		System.out.print(imgLength); //poste imglength
    		System.out.print(" "); // leerzeichen
    		System.out.print(countLines); //poste anzahl der zeilen
    		
    		} 
    		}
    This is the final code, all the inputs and outputs work perfectly...

    the error i oversaw was that i had to set the countLines int to 1 instead of 0


    Sweet...

    Thanks for whatever some of you people were claiming to be help :P

    Thanks also for the Array and the for loops :)

    Take care!

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

    Default

    Quote Originally Posted by Greygoose View Post
    Java Code:
    import java.util.Scanner;
    
    public class AsciiReader {
    	
    	public static void main(String args[]){
    		int imgLength = 0, countLines = 1;
    		String imgLine ="";
    		Scanner scan = new Scanner(System.in);
    		imgLine = scan.next();
    		imgLength = imgLine.length();
    		
    		while(scan.hasNext()){  // as long as there is input/Something to read in
    			
    			imgLine = scan.next(); // String imgLine gets fed the next line
    			countLines++; // Counter is increased by one for each line fed
    		
    		if(imgLine.length() != imgLength){ // if the read imgLine string length does not equal the imgLength integer
    			System.out.println("Input Mismatch"); // then abort by printing input mismatch and close
    			return; //brich ab
    		}
    		}
    		
    		System.out.print(imgLength); //poste imglength
    		System.out.print(" "); // leerzeichen
    		System.out.print(countLines); //poste anzahl der zeilen
    		
    		} 
    		}
    This is the final code, all the inputs and outputs work perfectly...

    the error i oversaw was that i had to set the countLines int to 1 instead of 0


    Sweet...

    Thanks for whatever some of you people were claiming to be help :P

    Thanks also for the Array and the for loops :)

    Take care!
    No problem :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. String comparison based on system locale
    By couling in forum Advanced Java
    Replies: 2
    Last Post: 02-14-2011, 10:03 PM
  2. String object comparison issue
    By subith86 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 07:18 AM
  3. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 09:11 AM
  4. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 05:47 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01: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
  •