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 12 of 12

Thread: Problem with string [...]

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Problem with string [...]

    Hello, I am new to Java and I've got a problem already.
    The program should copy person with the highest percent from the file 'plik1', and then put it to the file 'plik2'.
    The problem occurs while compiling.

    Thank you for understanding and your help,

    fkmk

    Compilator:

    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type
    	string cannot be resolved to a type

    Code:

    package zadanie2;
    import java.util.*;
    import java.io.*;
     
    public class zadanie2 {
    	public static void main(String[] args) throws Exception {
    		Scanner plik1 = new Scanner(new File("plik.txt"));
    		plik1.useLocale(Locale.US);
    		PrintWriter plik2 = new PrintWriter("plik_max.txt");
     
    		Person person[] = new Person[5];
     
    		System.out.println("List of people:");
     
    		while(plik1.hasNext()) {
    			for(int counter=0; counter<5; counter++) {
    			person[counter].name = plik1.next();
    			person[counter].surname = plik1.next();
    			person[counter].age = plik1.nextInt();
    			person[counter].percent = plik1.nextFloat();
    			System.out.println("%s %s %d %f", person[counter].name, person[counter].surname, person[counter].age, person[counter].percent);
    			counter++;
    			}
     
    			int counter = 0;
    	        float maximum = person[0].percent;
     
    	        for(counter=1; counter<5; counter++) {
    	        if(person[counter].percent > maximum)
    	        maximum = person[counter].percent;
    	        }
     
    	        char badge = '*';
     
    	        for(counter=0; counter<5; counter++) {
    	            if((person[counter].name.length() > 3) && (person[counter].surname.lastIndexOf("ski")) && (person[counter].percent == maximum)) {
    	            plik2.print(person[counter].name); plik2.print(person[counter].surname); plik2.println(person[counter].age);
    	            } }
    		}
    		plik1.close();
    		plik2.close();
    	}
    }
     
    class Person {
        public :
             string name;
             string surname;
             int age;
             float percent;
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with string [...]

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Java is case sensitive. 'string' is not the same as 'String'.

    By convention, class names in Java begin with capital letters.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    fkmk (March 23rd, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with string [...]

    Also posted at: Problem with string [...]
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string [...]

    Firstly thanks for your replies.
    Norm, I know, but I needed a quick reply. Sorry for that.

    I fixed the code, but another problem occured. Could you please take a look at this?
    Thanks in advance.

    fkmk

    Compilator:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The operator && is undefined for the argument type(s) int, boolean
     
    	at zadanie2.zadanie2.main(zadanie2.java:36)

    Code:

    package zadanie2;
    import java.util.*;
    import java.io.*;
     
    public class zadanie2 {
    	public static void main(String[] args) throws Exception {
    		Scanner plik1 = new Scanner(new File("plik.txt"));
    		plik1.useLocale(Locale.US);
    		PrintWriter plik2 = new PrintWriter("plik_max.txt");
     
    		Person person[] = new Person[5];
     
    		System.out.println("List of people:");
     
    		while(plik1.hasNext()) {
    			for(int counter=0; counter<5; counter++) {
    			person[counter].name = plik1.next();
    			person[counter].surname = plik1.next();
    			person[counter].age = plik1.nextInt();
    			person[counter].percent = plik1.nextFloat();
    			System.out.println("%s %s %d %f", person[counter].name, person[counter].surname, person[counter].age, person[counter].percent);
    			counter++;
    			}
     
    			int counter = 0;
    	        float maximum = person[0].percent;
     
    	        for(counter=1; counter<5; counter++) {
    	        if(person[counter].percent > maximum)
    	        maximum = person[counter].percent;
    	        }
     
    	        char badge = '*';
     
    	        for(counter=0; counter<5; counter++) {
    	            if((person[counter].name.length() > 3) && (person[counter].surname.lastIndexOf("ski")) && (person[counter].percent == maximum)) {
    	            plik2.print(person[counter].name); plik2.print(person[counter].surname); plik2.println(person[counter].age);
    	            } }
    		}
    		plik1.close();
    		plik2.close();
    	}
    }
     
    class Person {
        private :
             String name;
             String surname;
             int age;
             float percent;
    }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with string [...]

    Posting at multiple sites is allowed. Just tell us so we don't waste time answering a question that has been answered.

    The AND operator && is for boolean operands. To get a boolean operand from an int value, compare it: x != 0
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (March 23rd, 2014)

  8. #6
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string [...]

    Quote Originally Posted by Norm View Post
    Posting at multiple sites is allowed. Just tell us so we don't waste time answering a question that has been answered.

    The AND operator && is for boolean operands. To get a boolean operand from an int value, compare it: x != 0
    Ok, I understand. Sorry for that.

    I fixed the code following your tips guys, but now compilator shows:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
     
    	at zadanie2.zadanie2.main(zadanie2.java:6)

    Code:

    package zadanie2;
    import java.util.*;
    import java.io.*;
     
    public class zadanie2 {
    	public static void main(String[] args) throws Exception {
    		Scanner plik1 = new Scanner(new File("plik.txt"));
    		plik1.useLocale(Locale.US);
    		PrintWriter plik2 = new PrintWriter("plik_max.txt");
     
    		Person person[] = new Person[5];
     
    		System.out.println("List of people:");
     
    		while(plik1.hasNext()) {
    			for(int counter=0; counter<5; counter++) {
    			person[counter].name = plik1.next();
    			person[counter].surname = plik1.next();
    			person[counter].age = plik1.nextInt();
    			person[counter].percent = plik1.nextFloat();
    			System.out.printf("%s %s %d %f", person[counter].name, person[counter].surname, person[counter].age, person[counter].percent);
    			counter++;
    			}
     
    			int counter = 0;
    	        float maximum = person[0].percent;
     
    	        for(counter=1; counter<5; counter++) {
    	        if(person[counter].percent > maximum)
    	        maximum = person[counter].percent;
    	        }
     
    	        char badge = '*';
    	        String SubStr = new String("ski");
     
     
    	        for(counter=0; counter<5; counter++) {
    	            if((person[counter].surname.lastIndexOf(SubStr) != 0)  && (person[counter].percent == maximum) && (person[counter].name.length() > 3)) { 
    	            plik2.print(person[counter].name); plik2.print(person[counter].surname); plik2.println(person[counter].age);
    	            } } 
    		}
    		plik1.close();
    		plik2.close();
    	}
    }

    Thanks for all your pieces of advice!

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with string [...]

    Unresolved compilation problem:
    That does not say what the problem is. Did you leave off some of the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string [...]

    Quote Originally Posted by Norm View Post
    That does not say what the problem is. Did you leave off some of the error message?
    No, I did not.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with string [...]

    If that is all your compiler says then your compiler is not worth much.
    Try using the javac compiler in a command prompt window. It gives good error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (March 23rd, 2014)

  13. #10
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string [...]

    Norm, could you please tell me or give any tutorial, how to switch to javac compiler? I have looked up some sites, but haven't found any worth tips.

  14. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (March 23rd, 2014)

  16. #12
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string [...]

    Norm, thank you.

Similar Threads

  1. [SOLVED] string length problem
    By ma5sacre in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2014, 01:02 PM
  2. String Problem
    By driczdc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 8th, 2013, 11:07 PM
  3. Little String Problem
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2013, 08:02 PM
  4. [SOLVED] String problem
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2011, 08:49 PM
  5. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM