Results 1 to 5 of 5
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Issue In loop and variable

    Java Code:
    System.out.println("Enter Entry : ");
     Scanner iin=new Scanner(System.in);
    int arr=iin.nextInt();
    int ar[]=new int[arr];
    for(int i=0;i<arr;i++)
    {
    Scanner ini=new Scanner(System.in);
    String p1=ini.next();
    
    }
    for(int v=0;v<arr;v++)
    System.out.print("Person name : " + p1);
    "String p1=ini.next();" why this p1 is not print on next loop it showing some error...



    C:\Users\Ethics\Desktop>javac Cyber.java
    Cyber.java:39: error: cannot find symbol
    System.out.print("Person name : " + p1);
    ^
    symbol: variable p1
    location: class Cyber
    1 error

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Issue In loop and variable

    The p1 you are expecting is defined inside the for loop. That means the scope is the for loop only. Solution: declare the String p1 outside of the loop and assign it a value inside the loop. If it complains it's not initialized, initialize it to null 9or whatever default String you want) if you need to.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Issue In loop and variable

    Java Code:
    for(int i=0;i<arr;i++)
    
    {
    Scanner ini=new Scanner(System.in);
    String p1=ini.next();
    
    }
    String p1=" ";
    
    for(int j=0;j<arr;j++)
    	System.out.print("Person name : " + p1);
    Like this or any other method can you please define orr correct this..
    Thanks

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Issue In loop and variable

    There are more logic errors in your code, but this would fix the p1 issue.

    What are you trying to accomplish?

    Java Code:
    String p1 = "";
    
    for(int i=0;i<arr;i++){
      Scanner ini=new Scanner(System.in);
      p1=ini.next();
    }
    
    for(int j=0;j<arr;j++) {
        System.out.print("Person name : " + p1);
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Issue In loop and variable

    i'm learning java i'm newbie
    i'm trying to making program for Cyber Cafe in which Client Enter in program with a user name or password that are given by me and then they add entey of person cabin number or timing when it start surf internet ....soo i write this code
    Java Code:
    import java.util.*;
    
    
    
    public  class Cyber 
    {
    	
    	public static void main(String[] args)
    	{
    		String p;
    	System.out.println("Enter User Name : ");
    Scanner in=new Scanner(System.in);
    String us=in.next();
    String us1="vknehra10";
    
    if(us.equals(us1))
    {
      System.out.println("Enter Passcode : ");
    Scanner iin=new Scanner(System.in);
    int ps=iin.nextInt();
    int ps1=12345; 
    }
    
    else
    {
    	System.out.println("Good Bye");
    }
    System.out.println("Enter Entry : ");
     Scanner iin=new Scanner(System.in);
    int arr=iin.nextInt();
    int ar[]=new int[arr];
    for(int i=0;i<arr;i++)
    
    {
    Scanner ini=new Scanner(System.in);
    String p1=ini.next();
    
    }
    
    String p1=" ";
    
    for(int j=0;j<arr;j++)
    	System.out.print("Person name : " + p1);
    
    	}
    }

Similar Threads

  1. Issue with Java wanting a variable
    By Zoralink23 in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 03:33 AM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 8
    Last Post: 03-04-2011, 01:50 PM
  4. Issue with saving multiple times to a variable
    By sidd0123 in forum New To Java
    Replies: 3
    Last Post: 06-06-2010, 02:02 AM
  5. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 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
  •