यदि किसी सरणी का उपयोग दायरे में किया जाता है तो जावा में उसका मान कैसे शून्य हो जाता है? मैं इसके लिए जावा कोड प्रदान कर रहा हूं…


यहां उल्लिखित बाकी कोड सही हैं…

import java.io.File;
import javax.swing.JOptionPane;

class abc
{
    // Fall is a directory of files
    for(File fr:Fall.listFiles())
                        {
                             // jtxt is an object of JTextArea()
                            if((jtxt.getText().contains("select ")) && (jtxt.getText().contains(" from ")) && (!jtxt.getText().contains(",")) && (jtxt.getText().contains(fr.getName())) && (ke.getKeyCode()==10))
                            {
                              String strField=jtxt.getText().substring(7).strip();
                                String crunch="";
                                char []p=strField.toCharArray();
                                char []a=new char[p.length];
                                for(int i=0;i<p.length;i++)
                                {
                                    if(p[i]!=' ')
                                    {
                                        crunch +=p[i];
                                    }
                                    if(p[i]==' ')
                                    {
                                        break;
                                    }
                                }
                                a=crunch.toCharArray();
                                String [] cell=new String[1];
                                for(int i=0;i<a.length;i++)
                                {
                                    cell[0]+=a[i];
                                }
                                String [] cc=new String[cell.length];
                                cc[0]=cell[0].substring(4);
                            
                                JOptionPane.showMessageDialog(null, cc[0]);
                            
                                try
                                {
                                
                                    FileReader fread=new FileReader(fr.getAbsolutePath()+"//"+"tbl");
                                    BufferedReader in=new BufferedReader(fread);
                                    String sql="";int val=0;int k=0;
                                    while((sql=in.readLine())!=null)
                                    {
                                     
                                        char [] pp=sql.toCharArray();
                                        char []aa=new char[pp.length];
                                        String []field=new String[p.length];
                                        int j=0;
                                        for(int i=0;i<pp.length;i++)
                                        {
                                            if(pp[i]!='-')
                                            {
                                                aa[i]=pp[i];
                                                field[j]+=aa[i];
                                            }
                                            if(pp[i]=='-')
                                            {
                                                j++;
                                            }
                                        }
                                    
                                        
                                        for(int i=0;i<j;i++)
                                        {
                                            if(cell[0].equals(field[i]))
                                            {
                                                val=i;
                                            }
                                        }
                                        k++;
                                    }
                                    int ss=0;int t=0;
                                    try
                                    {
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);
                                        JOptionPane.showMessageDialog(null, val);
                                        String str="";
                                        ss=(int)in.lines().count();
                                        while((str=in.readLine())!=null)
                                        {
                                            t=in.readLine().length();
                                        }
                                        fread.close();
                                        in.close();
                                    }                                    
                                    catch(IOException e)
                                    {
                                        
                                    }
                                     JOptionPane.showMessageDialog(null,"ss "+ ss);
                                    String [][] g=new String[ss][t+250];
                                    String [][] Unit=new String[ss][t+250];
                                    String[] s=new String[ss];
                                    try
                                    {
                                        
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);int y=0;
                                        String str="";int n=0;int jj=0;
                                        while((str=in.readLine())!=null)
                                        {
                                            //JOptionPane.showMessageDialog(null, str);
                                            char[] u=str.toCharArray();
                                            char[] b=new char[u.length];
                                            
                                            
                                            for(int i=0;i<u.length;i++)
                                            {
                                                if(u[i]!='-')
                                                {
                                                    b[i]=u[i];
                                                    Unit[y][jj]+=b[i];
                                                }
                                                if(u[i]=='-')
                                                {
                                                  JOptionPane.showMessageDialog(null,"unit : "+ Unit[y][jj]);
                                                    jj++;JOptionPane.showMessageDialog(null, "y : "+y);
                                                }
                                            }
                                            y++;
                                             
                                        }
                                        str="";
                                        in.close();
                                        fread.close();n=0;
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);int b=1;int c=0;
                                        if(y==ss)
                                        {
                                        while(n<ss)
                                            {                                     
                                                for(int i=0;i<jj-1;i++)
                                                {                                                     
                                                    if(i==val)
                                                    {
                                                        g[n][i]=Unit[n][i];
                                                        JOptionPane.showMessageDialog(null, "g[c][i] : "+g[n][i]);
                                                        // the problem is here g[n][i] shows the first value correctly. but thenafter 
                                                        // it shows the values to be null, although the values are present in the file.
                                                    }                                                
                                                }                                               
                                                n++;c++;
                                            } 
                                        
                                        fread.close();
                                        in.close();
                                        n=0;
                                        
                                          
                                        
                                        
                                         TableViewStatic tvs=new TableViewStatic(g,cc,fr.getName());
                                           JTextFrame jf=new JTextFrame(tvs,jtxt,"SQL NoteBook");
                                           //These methods are correct
                                        }
                                    }
                                    catch(IOException e)
                                    {
                                            
                                    }
                                }
                                catch(IOException e)
                                {
                                    
                                }
                            }
                        }
}

मैंने क्या प्रयास किया है:

मैं समझ नहीं पा रहा हूं कि Unit[n][i] पहला मान दिखाने के ठीक बाद शून्य हो जाता है, जब रिकॉर्ड फ़ाइल में मौजूद होते हैं

समाधान 1

जैसा कि रिचर्ड ने कहा है, यह संसाधित डेटा पर निर्भर करेगा, और हमारे पास आपके फ़ाइल सिस्टम तक कोई पहुंच नहीं है – डिबगर यहां आपका सबसे अच्छा दोस्त है (हमेशा की तरह) क्योंकि यह आपको दिखा सकता है कि आपका प्रोग्राम चलने के दौरान वास्तव में क्या हो रहा है।
तो, यह आप पर निर्भर करेगा।

सौभाग्य से, आपके पास एक उपकरण उपलब्ध है जो आपको यह पता लगाने में मदद करेगा कि क्या हो रहा है: डिबगर। आप इसका उपयोग कैसे करते हैं यह आपके कंपाइलर सिस्टम पर निर्भर करता है, लेकिन आपके आईडीई और “डीबगर” के नाम के लिए एक त्वरित Google आपको आवश्यक जानकारी देगा।

फ़ंक्शन में पहली पंक्ति पर ब्रेकपॉइंट लगाएं, और डीबगर के माध्यम से अपना कोड चलाएं। फिर अपने कोड और अपने डेटा को देखें और पता लगाएं कि मैन्युअल रूप से क्या होना चाहिए। फिर एक चरण में प्रत्येक पंक्ति की जाँच करें कि आपने जो होने की उम्मीद की थी वही हुआ। जब ऐसा नहीं होता है, तब आपको कोई समस्या होती है, और आप इसका कारण जानने के लिए बैक-ट्रैक कर सकते हैं (या इसे फिर से चला सकते हैं और अधिक बारीकी से देख सकते हैं)।

क्षमा करें, लेकिन हम आपके लिए ऐसा नहीं कर सकते – यह आपके लिए एक नया (और बहुत, बहुत उपयोगी) कौशल सीखने का समय है: डिबगिंग!

लेकिन अपने आप पर दो बड़े उपकार करें:
1) वास्तव में छोटे वेरिएबल नामों का उपयोग करना बंद करें: “ए”, “पी”, “जेजे”, “वाई”, “सी” इत्यादि के बजाय सार्थक नामों का उपयोग करें और आपका कोड अधिक पठनीय हो जाता है, लेकिन अधिक विश्वसनीय भी हो जाता है चूँकि यह तब अधिक स्पष्ट होता है जब आप गलती से गलत वेरिएबल का उपयोग करते हैं…

2) अपवादों को निगलना बंद करें।

जावा
catch(IOException e)
{

}

एक त्रुटि हो सकती है “चले जाओ” लेकिन यह बहुत संभव है कि एक निगल लिया गया अपवाद उस समस्या का कारण बन रहा है जो आपने देखी है। यदि आप किसी त्रुटि को अनदेखा करना चाहते हैं, तो त्रुटि को किसी फ़ाइल में लॉग करें catch ब्लॉक करें ताकि यदि कुछ गलत हो तो आप पीछे मुड़कर देख सकें कि आपने किन समस्याओं को नज़रअंदाज़ किया।

コメント

タイトルとURLをコピーしました