Results 1 to 6 of 6
  1. #1
    vknehra10 is offline Member
    Join Date
    Dec 2016
    Posts
    37
    Rep Power
    0

    Default String Variable Issue In loop

    Java Code:
    System.out.println("Enter Client Names : ");
    String cn="";
    for(int i=0;i<arr;i++)
    {
    Scanner cni=new Scanner(System.in);
     cn=cni.next();
    System.out.println("Person"+cn);
    }
    for(int i=0;i<arr;i++)
    System.out.print("Client name : "+cn);
    I'm trying to print all my person names at last with "Client Name : blah1
    Client Name : blah2
    Client Name : blah2

    like this when i print "cn" it prints only last name entered by user
    for e.g

    if user enters 3 entry program ask "Enter client name"
    then user enter names like "vivek","Naina","Dev"
    So the last name entered by user this program or this loop "for(int i=0;i<arr;i++)
    System.out.print("Client name : "+cn);" print only last name "Dev"

    fix this logic error..
    thanks

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

    Default Re: String Variable Issue In loop

    Variable 'cn' is set to the next name on line #6; from then on, 'cn' never changes anymore, so the last entered name is printed over and over again on line #10.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: String Variable Issue In loop

    I have a feeling you want to add client names to an array. We could use a bit more background. Now it's just guesswork.

    In case I am right, here's the steps you want to follow:
    1) Declare the number of persons you want to add (I believe that's variable "arr" in your code.)
    2) Declare an array of Strings with the length of step 1
    3) Start a for loop that runs for "arr" times.
    Inside the loop:
    3.1) Declare a String and ask the user for a name
    3.2) Set the n-th element of the array to that name

    4) To print the results, start a new for loop that runs for "arr" times.
    Inside the loop:
    4.1) Print the n-th element from the array using System.out
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    vknehra10 is offline Member
    Join Date
    Dec 2016
    Posts
    37
    Rep Power
    0

    Default Re: String Variable Issue In loop

    String[] cn=new String[arr];
    for(int i=0;i<arr;i++)
    {
    Scanner key=new Scanner(System.in);
    String inp=in.next();
    inp[]=new String[i];
    }
    for(int i=0;i<arr;i++)
    System.out.println("blah :"+ inp[i]);


    like this

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,395
    Blog Entries
    7
    Rep Power
    25

    Default Re: String Variable Issue In loop

    Did you try to compile this mess? No? You shouldn't have posted it either.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: String Variable Issue In loop

    I was going to wrap that in code tags for you, but it appears there's no indentation in the original either.

    You need to learn to format your code correctly, as it makes it a lot easier to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Issue In loop and variable
    By vknehra10 in forum New To Java
    Replies: 4
    Last Post: 12-16-2016, 03:54 PM
  2. Replies: 10
    Last Post: 06-26-2014, 09:04 AM
  3. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  4. Replies: 10
    Last Post: 05-11-2011, 02:26 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Tags for this Thread

Posting Permissions

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