Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: PROBLEM WITH MY CODE!!!!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PROBLEM WITH MY CODE!!!!

    import java.io.*;
    import java.util.*;
    public class mine{

    public static void main(String args[])throws IOException{
    BufferedReader min = new BufferedReader(new InputStreamReader(System.in));
    Scanner in=new Scanner(System.in);

    int choice, quanti=0, loop = 1, counter = 0;
    String iname[]= new String[loop];;
    int quant[]= new int[loop];

    if(loop-1==counter)
    {

    do{

    loop+=1;


    System.out.println(counter);
    System.out.println(loop);
    System.out.println("----------------------------- ");
    System.out.println("Choose the desired action ");
    System.out.println("----------------------------- ");
    System.out.println("[1]Enter items ");
    System.out.println("[2]View Inventory ");
    System.out.println("[3]Exit ");
    System.out.println("----------------------------- ");
    choice = in.nextInt();



    if (choice == 1)
    {


    for(int x = 0; x <loop;x++) {


    if (iname[x]==null|| iname[x]=="")
    {
    System.out.println("Enter item name:");
    iname[x]= min.readLine();
    System.out.println("Enter quantity: ");
    quant[x]= in.nextInt();
    counter+=1;
    break;
    }

    if (iname[x]!=null|| iname[x]!="")
    {

    counter+=1;
    break;

    }

    else
    {
    continue;
    }
    }

    }

    if (choice == 2)
    {

    for(int b =0; b<counter;b++){
    System.out.println(quant[b]);
    System.out.println(iname[b]);

    }





    }
    }
    while(choice!=3);

    }




    }
    }


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PROBLEM WITH MY CODE!!!!

    i cant print the array in the second if statement of "choice".. please help.. im new to this...

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: PROBLEM WITH MY CODE!!!!

    If you want help, you'll have to create an SSCCE that demonstrates exactly what you're talking about. Don't forget the code tags.

    Also, I might suggest using a better title- all caps and no mention of the actual problem will actually decrease your chances of getting help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: PROBLEM WITH MY CODE!!!!

    import java.io.*;
    import java.util.*;
    public class mine{
     
      public static  void main(String args[])throws IOException{
        BufferedReader min = new BufferedReader(new InputStreamReader(System.in)); 
        Scanner in=new Scanner(System.in);
     
        int choice, quanti=0, loop = 1, counter = 0;
        String iname[]= new String[loop];;
        int quant[]= new int[loop];
     
        if(loop-1==counter)
        {  
     
          do{
     
            loop+=1;
     
     
            System.out.println(counter);
            System.out.println(loop);
            System.out.println("----------------------------- ");
            System.out.println("Choose the desired action ");
            System.out.println("----------------------------- "); 
            System.out.println("[1]Enter items ");
            System.out.println("[2]View Inventory ");
            System.out.println("[3]Exit ");
            System.out.println("----------------------------- ");
            choice = in.nextInt();
     
     
     
            if (choice == 1)
            {
     
     
              for(int x = 0; x <loop;x++) {
     
     
                if (iname[x]==null|| iname[x]=="")
                {
                  System.out.println("Enter item name:");
                  iname[x]= min.readLine();
                  System.out.println("Enter quantity: ");
                  quant[x]= in.nextInt();
                  counter+=1;
                  break;
                } 
     
                if (iname[x]!=null|| iname[x]!="")
                {
     
                  counter+=1;
                  break;
     
                }
     
                else 
                {
                  continue;
                }
              }
     
            }
     
            if (choice == 2)
            {
     
              for(int b =0; b<counter;b++){
                System.out.println(quant[b]); 
                System.out.println(iname[b]); 
     
              }
     
     
     
     
     
            }
          }
          while(choice!=3);
     
        }
     
     
     
     
      }
    }

    Much more readable this way
    EDIT: You have weird spacing.... are you using an IDE or just notepad? I used my IDE to indent it.

    What exactly is your problem? Have you already attempted using println's to find out what the code is actually doing (compared to what it should be). Specific questions get specific answers
    Last edited by Tjstretch; January 26th, 2012 at 10:26 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: PROBLEM WITH MY CODE!!!!

    Why do you have two classes for reading from the console? If one class reads and buffers the input, the other class will not know about it.

    iname[x]!=""
    You should use the equals method to compare Strings or use the length or is empty method.

Similar Threads

  1. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  2. I have a problem with my code I think
    By byrne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 12th, 2011, 04:18 AM
  3. problem with code
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 08:06 AM
  4. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM
  5. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM