-1

I want make code in which we have to add inputs and it show output at very after all inputs are performed. But i don't know how can i access the contents of variable in for loop in outer side.

My code is given below

import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
      
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();   //How many  Input case?
        for(int i=0;i<T;i++)
        {
            int N=sc.nextInt();        //How many items in each cases?
            for(int j=0;j<N;j++)
            {
                int C=sc.nextInt();   //How much cost of each items?
            }
        }
        int [][]A =new int[T][N];

        //I know here No issue with T but how can i acces N and C?

        //here code to show output of given inputs.....
....
..


    }
}

Ex:-

Input:--

2 //test cases

3  //items

1  2  3  //costs

4  //items

1 2 3 4  //costs


Ouput:---perfectly as in input.
shubh
  • 1
  • 2
  • I am note sure what you are asking for. Assuming that you want to create a 3 dimensional array, then you need 3 values, T, N, and C. So you ask for them before any looping, you create the array, and there you go. Also note: you want to research **scope**. A local variable is only visible in the SCOPE it is defined. When you define a variable inside a loop, it only exists WITHIN that loop body. – GhostCat Feb 08 '22 at 13:39
  • And then, are you sure that you really want a 3 dim array? It seems you have: n cases, each one has (the same) m items. And then the *cost* are the actual **values** in that 2 dim array? – GhostCat Feb 08 '22 at 13:42
  • Finally: give your variables meaningful names. Variable names should start lower case, and they should be meaningful to the human readers, like: `int cost[][] = new int[numberOfCases][numberOfItems]` . – GhostCat Feb 08 '22 at 13:44
  • I'm sorry but although a similar question may not be immediately found, this is kind of an "no own research" question. ANYWAY, therefore i will write the ANSWER as a comment: variables declared inside a loop or if block or similar, can NOT be accessed outside of it BUT you can simply use "Andrew"s answer OR EVEN SIMPLER just target the places inside the array so you'd **declare your array FIRST and then just go A[0]=sc.nextInt() and so on [1] and [2] inside the loops. P:S:: i dont know if loops are even necessary in your use case but this answers the actual question.** – LuckyLuke Skywalker Feb 08 '22 at 13:50
  • As said: turn to https://stackoverflow.com/questions/17997600/multidimensional-arrays-with-different-sizes ... you cant do it like that. You have to FIRST create a 3 dim array, and then you iterate all the rows, and **change** the rows. Remember, actually, java does NOT have multi dim arrays. Instead you have arrays that "point" to other arrays. Read that link I gave you. – GhostCat Feb 08 '22 at 14:01

2 Answers2

0

If I right understand what do you want, you should declare N and C at procedure begin:

import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
      
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();   //How many  Input case?
        int N=0;
        int C=0;
        for(int i=0;i<T;i++)
        {
            N=sc.nextInt();        //How many items in each cases?
            for(int j=0;j<N;j++)
            {
                C=sc.nextInt();   //How much cost of each items?
            }
        }
        int [][][]A =new int[T][N][C];

        //Now here T,N and C are accessible

        //here code to show output of given inputs.....
....
..


    }
}
Andrew
  • 175
  • 7
  • but in each cases N and C are diffrent. – shubh Feb 08 '22 at 13:46
  • Hint: words matter. This isn't a "procedure", it is a static method. And the real answer isn't to declare everything in the beginning of the method, but at the first point you need it. And seriously: the question is super unclear. Avoid answering such unclear things. You see, it really doesnt make sense to ask for N and C in a loop. – GhostCat Feb 08 '22 at 13:46
  • @shubh In that case, have a look at https://stackoverflow.com/questions/17997600/multidimensional-arrays-with-different-sizes – GhostCat Feb 08 '22 at 13:48
  • Also note: having a multi dim array where each row has different number of entries is pretty uncommon, and it will make your code VERY hard to maintain over time. The real answer would be: create a class that represents a single "Case", whatever that is. A Case class then might have an array of Item classes, and the Item holds the cost elements. And then you end up with just creating a 1-dim array of Case. – GhostCat Feb 08 '22 at 13:50
  • @shubh Again, it is really not clear what you want to achieve. Read [mcve], and read that link I gave you here a few minutes ago. It explains how you create a multi dim array where the rows have different numbers of entries. And as others have said: this is very basic stuff. Try reading a good book on java arrays. It will for sure cover all aspects of handling multi dimensional arrays. – GhostCat Feb 08 '22 at 13:54
  • @GhostCat now ,please have a look in my edited code – shubh Feb 08 '22 at 13:57
  • Do you want to print all input values, but only when input is finished at all? If yes? how do you understands that input finished? – Andrew Feb 08 '22 at 13:57
  • @shubn, why did you add cycles when you read dimensions? Your code creates only one result array. – Andrew Feb 08 '22 at 14:03
  • @Andrew yes andrew ,I exactly want to perform this action – shubh Feb 08 '22 at 14:04
0

I don't understand the question, but in any case your variables (except T) are out of scope. Should be something like (psueo-code)

import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
      
        int [][][] A;
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();   //How many  Input case? say 3
        for(int i=0;i<T;i++)
        {
            int N=sc.nextInt();        //How many items in each cases? say 14
            for(int j=0;j<N;j++)
            {
                int C=sc.nextInt();   //How much cost of each items? say 2
                A =new int[T][N][C]; // ok, you got a 3x14X2 array
                // now do here or outside what you want with it
            }
        }
        
        //I know here No issue with T but how can i acces N and C?
        // you can use A 3 dimensional array  here

        //here code to show output of given inputs.....
....
..


    }
}
LongChalk
  • 783
  • 8
  • 13
  • *Another thing I wondered about is how the same function (NextInt?) produces different results.* Using the scanner to read in multiple values is one of the most basic things, stuff that you learn like in your early exercises. So, take this as opportunity to read the javadoc for Scanner class and learn something. There is really no point in answering an unclear question and adding "yeah, I dont know that stuff here" to that. – GhostCat Feb 08 '22 at 13:52
  • @shubh No, it is NOT. because this will THROW away all the A array instances that you crea#ted before. This is NOT what you want. – GhostCat Feb 08 '22 at 14:02
  • 1
    @GhostCat You are right. You got me. Java is not my primary programming language. – LongChalk Feb 08 '22 at 14:03
  • @GhostCat could you share your code please? – shubh Feb 08 '22 at 14:21
  • @LongChalk Every time your code will create the new instance of A. – shubh Feb 08 '22 at 14:25
  • @shubh I gave you a link to another SO question that SHOWS you how to go about this problem multiple times by now. You really should not expect that people write down something for you AGAIN that A) you can find in any good book on the subject and B) that has been written down here before (many times). – GhostCat Feb 08 '22 at 14:57
  • 1
    @GhostCat thank you very much bro, got it what you want convey ...first make three dimensional array .and set its size as T (in my case) .Then go into for loop and make A[T]=new int[N] (that's number of items)...By doing this we can set size specifically in X for Y(first " []" is X and second " []" is Y like maths matrix). ...Got it bro – shubh Feb 08 '22 at 16:31