Results 1 to 8 of 8
  1. #1
    M ALVI is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Question error with sub string of tow variables

    i wrote this cod:
    Java Code:
    class L6T3{
    	public static void main(String[]args){
    		int i,j ;
    		String s ="0000000000000000";
    		String sp="                ";
    		for (i=0,j=4;i<=4;i++){
    		System.out.println(sp.substring(0,j));
    		System.out.println(s.substring(0,i));
    		System.out.println("1");
    		System.out.println(s.substring(0,i));
    		j--;
    	}
    }
    }
    which soppose to give me output like :

    1
    010
    00100
    0001000
    000010000

    but i tried to make i =1 but it does not fit too ...any one can help ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: error with sub string of tow variables

    Why is line #7 in your code? Variable j never changes so each time that line is executed, four 0s are printed ... you want a line with i zeros, a single 1 and 1 zeros again. i runs from 0 to n; this begs for a single loop that increments i and a separate thing (a method mayhap?) that produces i zeros ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,082
    Rep Power
    4

    Default Re: error with sub string of tow variables

    goodmorning Jos. Take a look at line #11.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: error with sub string of tow variables

    Quote Originally Posted by gimbal2 View Post
    goodmorning Jos. Take a look at line #11.
    Darn, that's because I had only one cup of espresso; I need a refill ... (but most of my previous reply still stands)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,082
    Rep Power
    4

    Default Re: error with sub string of tow variables

    Couldn't agree more. At this point there is code that simply does not do what it is supposed to do. So understand WHY the current code is wrong, and then write code which does in fact do what it is supposed to do. Take it one step at a time: create some code which produces this:

    Java Code:
    1
    01
    001
    0001
    00001
    then take it from there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    M ALVI is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: error with sub string of tow variables

    you mean like
    Java Code:
    class Zeros{
    	public static void main(String[]args){
    		int i ;
    		String s ="0000000000000000";
    		for(i=0;i<=10;i++)
    		System.out.println(s.substring(0,i)+1+s.substring(0,i));
    	}
    }
    
    then what ??

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,082
    Rep Power
    4

    Default Re: error with sub string of tow variables

    I am not going to say anything more if you are just going to try stuff and hope for the best. Think about it first. And while you're at it: throw your current code away and start fresh. You do not need a long String of zeroes to print a line of smaller zeroes, you also don't need substring.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: error with sub string of tow variables

    besides all that, try an arbitrary large upperbound for i, say, 42 and see what happens ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Splitting String and assigning tokens to variables
    By igorland in forum New To Java
    Replies: 3
    Last Post: 02-06-2013, 02:47 AM
  2. Replies: 1
    Last Post: 06-29-2011, 05:46 PM
  3. String named variables
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 04:06 AM
  4. parse string into 2 double variables
    By woodsie2523 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 01:46 PM
  5. Displaying variables in a string?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 03:10 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
  •