proiect2 package;
import java.io. *;

public class String {
	static BufferedReader buffer = new BufferedReader (new InputStreamReader (System.in));
	static String string = null;
	public static void main () throws IOException, NumberFormatException {
		System.out.println ("Enter string:");
		String string = buffer.readLine ();
		while (sir.length () == 0) {
			System.out.println ("The string is empty");
			System.out.println ("Enter a new string:");
			string = buffer.readLine ();
			}
		if (sir.length ()> 0) {
			try {
				int i = Integer.parseInt (string);
				System.out.println ("String:" + string);
				System.out.println ("Integer:" + i);
				}
			catch (NumberFormatException NFE) {
				System.out.println ("The string contains invalid characters" + nfe.getMessage ());
				}
			}
		}
	}

I must write the JUnit tests, for this java code. But I'm stuck and I don't know how to go further.
A JUnit test, should look like this java code

package proiect2;

import static org.junit.Assert.*;
import org.junit.Test;

public class test1 {

	@Test
	public void testMain() {
		// ??
	}

}

You must first identify what functionality of the class you are trying to test. What you write depends on how you want to test the class. I'm not an expert on JUnit testing, but this article might help a lot.

Well your problem is that whole code is cramped in main method with no other method calls that is in general sign of bad codding and there is nothing to really test.

You should have at least two methods: 1) to read input provided by user and return value only if there is something to return (running while if input empty); 2) to have parse string.

Then you will write appropriate test for each of them like: no user input; user entered string; parse fail string not an integer; parse success string is possible to convert to integer.

proiect2 package;

import java.io. *;
java.text.NumberFormat import;
java.text.ParsePosition import;

public class String {
static BufferedReader buffer = new BufferedReader (new InputStreamReader (System.in));
static String string = null;
static boolean sirVid = false;

public void Read () throws IOException {
System.out.println ("Enter string:");
String string = buffer.readLine ();
if (SirVid (string) == true) {
System.out.println ("entered string is empty");
System.out.println ("Re-string:");
string = buffer.readLine ();
}
else if (SirVid (string) == false & & esteNumar (string) == false) {
System.out.println ("The string entered is not valid!");
System.out.println ("Re-string:");
string = buffer.readLine ();
}
}

public static boolean SirVid (String string) {
if (sir.equals ("")) {
sirVid = true;
}
sirVid return;
}

public static boolean esteNumar (String string) {
NumberFormat format = NumberFormat.getInstance ();
Position = new ParsePosition ParsePosition (0);
format.parse (string, position);
sir.length return () == pozitie.getIndex ();
}

StringToInt public void () throws IOException {
String string = buffer.readLine ();
int i = Integer.parseInt (string);
System.out.println ("String:" + string);
System.out.println ("Integer:" + i);
}

public static void main (String [] args) throws NumberFormatException, IOException {
String s = new String ();
int option;
do {
System.out.println ();
System.out.println ("You want to make a conversion String - Integer?");
System.out.println ("1. Yes");
System.out.println ("2. Not");
System.out.println ("Response");
option = Integer.parseInt (buffer.readLine ());
switch (option) {
case 1: s.Read ();
s.StringToInt ();
break;
case 2: System.out.println ("Exit.");
break;
default: System.out.println ("You have not entered correctly!");
break;
}
} While (option! = 2);
}
}

]

I've made this. But also doesn't work... any solution?

What you expect when you add import keyword at the end

java.text.NumberFormat import;

needs to be as

import java.text.NumberFormat;

and then also mess up method keywords order

StringToInt public void () throws IOException

should be something as

public void StringToInt() throws IOException

also that method should receive string as parameter and no call global variable

package proiect2;

import java.io.*;

public class Sir {
	static BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
	static String sir=null;
	
	public void Citire(String sir) throws IOException {	
		System.out.println("Introduceti sirul: ");
	    sir=buffer.readLine();
	    while (TestNumar(sir)==false) {
			System.out.println("Sirul introdus este invalid!");
			System.out.println("Reintroduceti sirul: ");
			sir=buffer.readLine();
			System.out.println();
			}
	    if (TestNumar(sir)==true) {
	    	StringToInt(sir);
	    }
	}
	
	public static boolean TestNumar(String sir) {
		try {
			Integer.parseInt(sir);
			} catch (NumberFormatException ex) {
				return false;
				}
		return true;
		}

	public void StringToInt(String sir) throws IOException {
		 if (TestNumar(sir)==true) {
			 int i = Integer.parseInt(sir);
			 System.out.println("Sir: " + sir);
			 System.out.println("Intreg: " + i);
		 }
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		Sir s=new Sir();
		s.Citire(sir);
		}
	}

I've updated my code. It works, but I must make the method StringToInt, whitout using parseInt(). This is a task that the program must have. The conversion from String to Integer, must be done using another method, not the parseInt(). I think I'm stuck again. Any help? I'm also a beginner in Java, so please have patience with me. Thanks! :) PS: My program must make a String to Integer conversion!

package proiect2;

import java.io.*;

public class Sir {
	static BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
	static String sir=null;
	
	public void Citire(String sir) throws IOException {	
		System.out.println("Introduceti sirul: ");
	    sir=buffer.readLine();
	    while (TestNumar(sir)==false) {
			System.out.println("Sirul introdus este invalid!");
			System.out.println("Reintroduceti sirul: ");
			sir=buffer.readLine();
			System.out.println();
			}
	    if (TestNumar(sir)==true) {
	    	StringToInt(sir);
	    }
	}
	
	public static boolean TestNumar(String sir) {
		try {
			Integer.parseInt(sir);
			} catch (NumberFormatException ex) {
				return false;
				}
		return true;
		}

	public void StringToInt(String sir) throws IOException {
		 if (TestNumar(sir)==true) {
			 int i = Integer.parseInt(sir);
			 System.out.println("Sir: " + sir);
			 System.out.println("Intreg: " + i);
		 }
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		Sir s=new Sir();
		s.Citire(sir);
		}
	}

I've updated my code. It works, but I must make the method StringToInt, whitout using parseInt(). This is a task that the program must have. The conversion from String to Integer, must be done using another method, not the parseInt(). I think I'm stuck again. Any help? I'm also a beginner in Java, so please have patience with me. Thanks! :) PS: My program must make a String to Integer conversion!

Heres two links that may be of use: http://www.coderanch.com/t/383469/java/java/Convert-String-primitive-int-without and http://www.codingforums.com/archive/index.php/t-210799.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.