• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Creation and debugging.  RSS feed

 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a few issues with this code I'm working on for my next assignment. Heres the assigment:
___________________________________________________________________________

Write class count, program has two constructors:

(1) default constructors set the output type to arabic
(2) constructor with one string argument either "arabic", "roman" or "english"

program has three counting methods:

(1) myForCount , (2)myWhileCount, (3)myDoWhileCount,

each method counts from 1 to N (where N can be between 1 and 10).
Each method will use the respective, for, while and do while loop.

Output of the program can be in one of three formats: Arabic 1..10, roman I..X, or English text one..ten. Type of format will be determined by value of the instance variable set by constructor during instantiation time. Hint all you need to do is setup up one 2-dimensional array of type string (i.e., String[][] output).

_________________________________________________________________________

heres what I have so far. In a nutshell, the teacher hasn't gone over creating methods, and I've cobbled what I have based on what bits I could find and interpret, my book, and some friends general help. Right now at compile I get 4 errors:

Count.java:9: <identifier> expected
System.out.println("Enter a number from one to ten.");
^
Count.java:10: <identifier> expected
countTo = keyboard.nextInt();
^
Count.java:11: <identifier> expected
System.out.println("Do you want the output in Arabic, Roman, or English?");
^
Count.java:12: <identifier> expected
numberOutput = keyboard.nextLine();

so, any thoughts/help/advice would be much appreciated.

import java.util.*;

public class Count
{
String numberOutput;
int languageType, loopCounter, countTo, column;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number from one to ten.");
countTo = keyboard.nextInt();
System.out.println("Do you want the output in Arabic, Roman, or English?");
numberOutput = keyboard.nextLine();


public String[][] count = {
{"One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"},
{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"},
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}};

public void myForLoop(String count[][])
{
for (column = 0; column < countTo; column++);
System.out.println(count[languageType][column]);
}

public void myWhileLoop(String count[][])
{
column = 0;
while (column < countTo)
{
System.out.println(count[languageType][column]);
countTo++;
}
}

public void myDoWhileLoop(String count[][])
{
column = 0;
do
{
System.out.println(count[languageType][column]);
countTo++;
}while (column < countTo);
}

public static void main(String[] args)
{


languageType = 2;//defalt is 2, which will point to Arabic for output

if (numberOutput.equalsIgnoreCase("english"))//determines which language the output should be in and sets
languageType = 0; //and sets a variable to tell us which column of the array to index

else if (numberOutput.equalsIgnoreCase("roman"))
languageType = 1;

countTo.myForLoop(count);
countTo.myWhileLoop(count);
countTo.myDoWhileLoop(count);
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you don't place regular code outside of methods or blocks in the class definition.

You can either move the code inside the main method, or place it inside a method and call that method from main.
 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, originally I had it inside main. Here is what I get at compile from that:

Count.java:18: illegal start of expression
public String[][] count = {
^
Count.java:23: illegal start of expression
public void myForLoop(String count[][])
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance variable declarations do not go inside methods.

But the code where the errors are appearing should be inside a method or block.

That is this code



would normally go inside the main method or another method that you call from main.
[ September 13, 2006: Message edited by: Keith Lynn ]
 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then I end up with:

Count.java:44: non-static variable countTo cannot be referenced from a static context
countTo = keyboard.nextInt();
^
Count.java:46: non-static variable numberOutput cannot be referenced from a static context
numberOutput = keyboard.nextLine();
^
Count.java:48: non-static variable languageType cannot be referenced from a static context
languageType = 2;
^
Count.java:49: non-static variable numberOutput cannot be referenced from a static context
if (numberOutput.equalsIgnoreCase("english"))
^
Count.java:50: non-static variable languageType cannot be referenced from a static context
languageType = 0; ^
Count.java:51: non-static variable numberOutput cannot be referenced from a static context
else if (numberOutput.equalsIgnoreCase("roman"))
^
Count.java:52: non-static variable languageType cannot be referenced from a static context
languageType = 1;
^
Count.java:53: non-static variable count cannot be referenced from a static context
countTo.myForLoop(count);
^
Count.java:53: non-static variable countTo cannot be referenced from a static context
countTo.myForLoop(count);
^

Count.java:54: non-static variable count cannot be referenced from a static context
countTo.myWhileLoop(count);
^
Count.java:54: non-static variable countTo cannot be referenced from a static context
countTo.myWhileLoop(count);
^

Count.java:55: non-static variable count cannot be referenced from a static context
countTo.myDoWhileLoop(count);
^
Count.java:55: non-static variable countTo cannot be referenced from a static context
countTo.myDoWhileLoop(count);
^

How can I fix this?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there is a reason you need a variable as a class level variable, you can declare them as local variables.

Otherwise, you could make the variables static, or use an instance of the class to access the instance variables.

Also remember that if you move a variable definition inside a method, you don't use the modifiers public or static on them.
[ September 13, 2006: Message edited by: Keith Lynn ]
 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, well, I just made the variables static and that fixed that problem. Thanks.

Now I have a problem with the scanner. When I run it, I'm prompted for the first line correctly, but after it print the next question asking for input it just automatically runs the rest of the code and skips and user input. Any clue why its doing this?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using nextLine, try using next when you try to read the type of output.

According to the API, next may block while waiting for input. But it doesn't say that about nextLine
[ September 13, 2006: Message edited by: Keith Lynn ]
 
Tony Krahnke
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you will need to add the constructors for the class, then in main() instantiate an instance of the Count class. From there you should be able to call your class methods. Here's a slightly modified version:

import java.util.*;

public class Count {
private static String m_ENGLISH = "english";
private static String m_ARABIC = "arabic";
private static String m_ROMAN = "roman";
private String[][] count = {{"One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"},
{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"},
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}};

String numberOutput;
int languageType;

Count(){
super();
languageType = 2;
}

Count(String sLanguage){
super();
if (sLanguage==m_ENGLISH){
languageType = 0;
}else if(sLanguage == m_ROMAN){
languageType = 1;
}else {
languageType = 2;
}
}

public void setLanguageType(int iLanguage){
languageType = iLanguage;
}


public void myForLoop() {
for (int i= 0; i < count[languageType].length; i++){
System.out.println(count[languageType][i]);
}
}

public void myWhileLoop() {
int i = 0;
while (i < count[languageType].length) {
System.out.println(count[languageType][i]);
i++;
}
}

public void myDoWhileLoop(){

int i = 0;
do{
System.out.println(count[languageType][i]);
i++;
}while (i< count[languageType].length);
}

public static void main(String[] args){
Count counter = new Count();
counter.myDoWhileLoop();
counter.myForLoop();
counter.myWhileLoop();

Count counter2 = new Count("english");
counter2.myDoWhileLoop();
counter2.myForLoop();
counter2.myWhileLoop();


}
}
 
Lucas Laws
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Aparently if you use nextInt() before you use nextLine(), its just skips any user input for the nextLine(). I switched them so the nextLine() happens first and everything compiled fine. Heres my final result.

Any tips on how to better code this are appreciated.

import java.util.*;

public class Count
{
static String numberOutput;
static int languageType, loopCounter, countTo, column;

static public String[][] count = {
{"One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"},
{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"},
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}};

public static void myForLoop(String count[][])
{
for (column = 0; column < countTo; column++)
System.out.println(count[languageType][column]);
if (column == countTo)
System.out.println("This was For\n");
}

public static void myWhileLoop(String count[][])
{
column = 0;
while (column < countTo)
{
System.out.println(count[languageType][column]);
column++;
if (column == countTo)
System.out.println("This was While\n");
}
}

public static void myDoWhileLoop(String count[][])
{
column = 0;
do
{
System.out.println(count[languageType][column]);
column++;
if (column == countTo)
System.out.println("This was Do While \n");
}while (column < countTo);
}

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Do you want the output in Arabic, Roman, or English?");
numberOutput = keyboard.nextLine();
System.out.println("Enter a number from one to ten.");
countTo = keyboard.nextInt();

languageType = 2;//defalt is 2, which will point to Arabic for output
if (numberOutput.equalsIgnoreCase("english"))//determines which language the output should be in and sets
languageType = 0; //and sets a variable to tell us which column of the array to index
else if (numberOutput.equalsIgnoreCase("roman"))
languageType = 1;
myForLoop(count);//fix me
myWhileLoop(count);
myDoWhileLoop(count);
}
}
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!