Semantic Error or Debugger problem?

Eclipse not showing variable values for a class when running in Debug

Page 1 of 1

9 Replies - 1769 Views - Last Post: 02 February 2009 - 05:34 PM Rate Topic: -----

#1 Krunktarheel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-February 09

Semantic Error or Debugger problem?

Post icon  Posted 02 February 2009 - 11:35 AM

Hey all,

I'm not quite sure what the problem is with my code. This is supposed to take a string argument from a command line. Scan the string then store the words or numbers into an array in String form. It is assumed that there will never be a situation where there are letters and numbers together (like "abc123") and there are space after every word or number.

EDIT: the commented out code is an artifact of the old way I tried to do it. Please ignore it.
EDIT 2: Also, I realize there are easier ways to do this, but I'm not allowed to use many library functions.

package comp401project;

public class NumberAndWordScanner {
	
	public final int MAX_TOKENS = 50;
	String Input = "";
	String[] TokenArray = new String[MAX_TOKENS];
	int ArrayPosition = 0;
	
	 
	public String getInput(){
		return Input;
	}
	
	public void setInput(String newInput){
		Input = newInput;
		initializeArray();
		parseInputForArray(newInput);
	}
	
	public String[] getTokenArray(){
		return TokenArray;
	}
	
	public boolean DigitCheck(char c){ //check to see if current character is a digit
		Character.isDigit(c);
		return true;
	}
	
	public boolean LetterCheck(char c){ //check to see if current character is a letter
		Character.isLetter(c);
		return true;
	}
	
	public void initializeArray(){ //initializes the values of TokenArray so ObjectEditor can interact with it.
		for(int i = 0; i < MAX_TOKENS; i++)
			TokenArray[i]="";
	}
	
	public void parseInputForArray(String str){ //go through the string and find words and numbers then send them to TokenArray[]
		
		int beginIndex = 0;  //This is the first index for the String.substring() method
		int finalIndex = 1;  //This is the second index for the String.substring() method
		str= " " + str;
		int i = 0;
		
		for(i = 1; i < (str.length()-1); i = i+1){
			
			char currentChar = str.charAt(i);
			char nextChar = str.charAt(i+1);
			char previousChar = str.charAt(i-1);
			
			//int count = 1;   //This counts the number of consecutive chars that are not whitespace. Will be added to beginIndex to receive finalIndex
			
			if(Character.isWhitespace(currentChar) && i == (str.length())){
				break;
			}
			/*if(!Character.isWhitespace(currentChar) && i==0){
				beginIndex = 0;
				continue;
			}	*/
			if(LetterCheck(currentChar) && !LetterCheck(previousChar)){
				beginIndex = i;
			}
			if(DigitCheck(currentChar) && !DigitCheck(previousChar)){
				beginIndex = i;
			}
			/*if(DigitCheck(currentChar) && DigitCheck(nextChar)){ //if there are 2 digits in a row count the number of digits
				count = count + 1;
			}
			if(LetterCheck(currentChar) && LetterCheck(nextChar)){//if there are 2 letters in a row count the number of letters
				count = count + 1;
			}*/
			if(DigitCheck(currentChar) && !DigitCheck(nextChar)){
				finalIndex = nextChar;
				TokenArray[ArrayPosition] = str.substring(beginIndex, finalIndex);
				ArrayPosition = ArrayPosition +1;
			}
			if(LetterCheck(currentChar) && !LetterCheck(nextChar)){
				finalIndex = nextChar;
				TokenArray[ArrayPosition] = str.substring(beginIndex, finalIndex);
				ArrayPosition = ArrayPosition +1;
			}	
		}
	}
	
	public void printArray() {
		for (int i = 0; i < TokenArray.length; i=i+1) {
			System.out.print(TokenArray[i]);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(args[0]);
		NumberAndWordScanner NumWordScan = new NumberAndWordScanner();
		NumWordScan.setInput(args[0]);
		NumWordScan.printArray();

	}
} 

This post has been edited by Krunktarheel: 02 February 2009 - 12:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Semantic Error or Debugger problem?

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 01:59 PM

If you know that there'll be only words or only numbers then can't you just check the first character of args and then treat the input thereafter. Otherwise I'm not sure what the question is, can you post a more detailed question?
Was This Post Helpful? 0
  • +
  • -

#3 Krunktarheel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-February 09

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 02:38 PM

View PostGloin, on 2 Feb, 2009 - 12:59 PM, said:

If you know that there'll be only words or only numbers then can't you just check the first character of args and then treat the input thereafter. Otherwise I'm not sure what the question is, can you post a more detailed question?


Sorry, let me try to be more clear. You enter a string: "abc 123 5 hey". The task is to take each individual serious of letters/words and numbers, get the substring() for example "abc" "123" "5" "hey". Place the substring into an array then print the array. The problem I'm having is that I've coded what I think will work, but the debugger will not show me why it is not working. I can't view what the values of the variables aside from main args[0] variable, and that makes it hard to see what's going on.

EDIT: It is assumed that every "Token" will have a space after it.

This post has been edited by Krunktarheel: 02 February 2009 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 02:42 PM

if you wanna break the string into different strings using space as a delimiter then just use the split method
String s = "abc 123 5 hey";
String [] a = s.split();


then you can loop through the array and display the different strings
Was This Post Helpful? 0
  • +
  • -

#5 Krunktarheel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-February 09

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 02:48 PM

View Postmostyfriedman, on 2 Feb, 2009 - 01:42 PM, said:

if you wanna break the string into different strings using space as a delimiter then just use the split method
String s = "abc 123 5 hey";
String [] a = s.split();


then you can loop through the array and display the different strings


I can't use the split method. I can only use charAt(), substring, length, isWhitespace, isDigit, & isLetter.
Was This Post Helpful? 0
  • +
  • -

#6 johnmalloy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 24-September 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 02:54 PM

Create a loop and have charAt() see if the char is equal to ''. that should get you started.
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 02:58 PM

import java.util.Scanner;
public class dowhile
{
	public static void main(String[]args)
	{
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		String x = "";
		int i = 0;
		while(i < s.length())
		{
			x = "";
			if(s.charAt(i) == ' ')
			{
				i++;
				continue;
			}
			do
			{
				x += s.charAt(i);
				i++;
			}
			while(i < s.length() && s.charAt(i) != ' ');
			System.out.println(x);		
		}
	}

}


this will split the string using space as a delimiter...mess around with it, try to put the split string into an array or whatever you wanna do with it.. good luck :)

This post has been edited by mostyfriedman: 02 February 2009 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 Krunktarheel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-February 09

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 03:05 PM

Will this work even if I can't use the Scanner library?
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 03:11 PM

yes, the Scanner library is just for input..you can have your input using any library you want and it will still work
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Semantic Error or Debugger problem?

Posted 02 February 2009 - 05:34 PM

StringTokenizer st = new StringTokenizer(args[0]);
String[] strArray = new String[st.countTokens());
for(int i = 0; i < strArray.length; i++)
strArray[i] = st.nextToken();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1