0

I'm writing a class that will build a SQL table create statement. what I'd like to do is call a method something like createTable(String tableName ColAndTypes... ct ). When I write the method I don't get any compile errors. I'm having trouble passing the values into the method when I call it though and I think it's because my syntax is wrong and I'm not sure how to fix it. I was wondering if you could look at the example I have provided and let me know what I need to do to fix it. Thanks so much for your help!

import java.util.*;
public class foo
{
public class bar{
    public String sBar1, sBar2;
    public bar(){
        sBar1 = "null";
        sBar2 = "null";
    }
    public bar(String sBar1, String sBar2){
    this.sBar1 = sBar1;
    this.sBar2 = sBar2;
    }
}

String sFoo;
List<bar> bi;

public foo(){
    sFoo = "null";
    bi = new bar();
}
public foo(Strinf sFoo, bar bi){
    this.sFoo = sFoo;
    this.bi = bi;
}

public void runFooBar(String sFoo, bar... barArgs)
{
    this.sFoo = sFoo;
    for(bar x:barArgs){System.out.Println(bi.get(x).sBar1   + ":" + bi.get(x).sBar2);}
}

public static void main(String[] args)  
{
    foo fi = new foo();
    fi.runFooBar("foo 1", ("1 sBar1","1 sBar2"),("2 sBar1 ","2 sBar2"))

}//end main

}//end class
3
  • bi is a List but you're initializing it as a bar. You have a typo Strinf instead of String. List#get(int) expects an int, but you are passing it a bar. Commented Mar 29, 2014 at 22:37
  • Please stick to Java naming conventions. Classes should be in PascalCase, so Bar rather than bar. To answer your specific question, you don't. We'll not unless you are using Java 8. To solve your current problem pass in instances of your (renamed) Bar class. Commented Mar 29, 2014 at 22:39
  • I see I do have some typos, thanks for the feedback Commented Mar 29, 2014 at 22:58

1 Answer 1

1

I'm not entirely sure what you're trying to do, but this fixes your syntax errors.

import java.util.ArrayList;
import java.util.List;

public class Foo {
    public static class Bar {
        public String sBar1, sBar2;

        public Bar(String sBar1, String sBar2) {
            this.sBar1 = sBar1;
            this.sBar2 = sBar2;
        }
    }

    String sFoo;
    List<Bar> bi;

    public Foo() {
        bi = new ArrayList<>();
    }

    public Foo(String sFoo, List<Bar> bi) {
        this.sFoo = sFoo;
        this.bi = bi;
    }

    public final void runFooBar(String sFoo, Bar... barArgs) {
        this.sFoo = sFoo;
        for (Bar x : barArgs) {
            System.out.println(x.sBar1 + ":" + x.sBar2);
        }
    }

    public static void main(String[] args) {
        Foo fi = new Foo();
        fi.runFooBar("foo 1", new Bar("1 sBar1", "1 sBar2"), new Bar("2 sBar1", "2 sBar2"));

    }//end main
}//end class
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I really appreciate your help. Looking at your corrections I can see where I made my mistakes!
I'm glad I could help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.