Splitting a String and Returning Converted Data
I have 2 Classes, the first one is only used as Container,
and the second Class Splits a GpsString, Converts it to suitable data types and returns the Container Class.
The String to analyze looks about like this:
("$GPGGA,120003.533,7926.184,N,02222.419,W,1,12,1.0,88.5,M,0.0,M,,*71")
Now i want to know if this is good practice in ObjectOriented Programming,
or if there is a better way for this Problem.
final class GpsData {
int Hours;
int Minutes;
double Seconds;
double Lattitude;
char DirectionLattitude; // North or South
double Longitude;
char DirectionLongitude; // North or South
int Quality;
int Satelites;
}
class ProtocolAnalyzer {
private GpsData AnalyzedData = new GpsData();
boolean AnalyzeData(String GpsData){
GpsData Gps = new GpsData();
int Attribute = 0;
StringBuilder WorkString = new StringBuilder();
int beginIndex = 0;
int length = GpsData.length();
for (int i = 0;i<=length;i++){
if(i == GpsData.length() ||
GpsData.charAt(i) == ','){
String Data = GpsData.substring(beginIndex,i);
boolean bool = ConvertData(Data,Attribute,Gps);
if(!bool) return false;
beginIndex = i + 1;
Attribute ++;
}
}
setAnalyzedData(Gps);
return true;
}
private boolean ConvertData(String Data,int Attribute,GpsData Gps){
boolean b = false;
switch (Attribute) {
case 0: // §GPGGA
if(Data.equals("$GPGGA")) {
b = true;
}
break;
case 1: //HHMMSS.sss
if(Data.length() == 10) {
b = true;
String split = Data.substring(0,2);
Gps.Hours = Integer.parseInt(split);
split = Data.substring(2,4);
Gps.Minutes = Integer.parseInt(split);
split = Data.substring(4,10);
Gps.Seconds = Double.parseDouble(split);
}
break;
case 2: //BBBB.BBBB
if (Data.length() != 0) {
b = true;
Gps.Lattitude = Double.parseDouble(Data);
}
break;
case 3: //b
if (Data.length() != 0) {
char DirLat = Data.charAt(0);
if (DirLat == 'N' ||
DirLat == 'S') {
b = true;
Gps.DirectionLattitude = DirLat;
}
}
break;
case 4: //LLLLL.LLLL
if (Data.length() != 0) {
b = true;
Gps.Longitude = Double.parseDouble(Data);
}
break;
case 5: //l
if (Data.length() != 0) {
char DirLong = Data.charAt(0);
if (DirLong == 'E' ||
DirLong == 'W') {
b = true;
Gps.DirectionLongitude = DirLong;
}
}
break;
case 6: //Q
if (Data.length() != 0) {
int Quality = Integer.parseInt(Data);
Gps.Quality = Quality;
if(Quality != 0) {
b = true;
}
}
break;
case 7: //NN
if (Data.length() != 0) {
int Satellites = Integer.parseInt(Data);
Gps.Satelites = Satellites;
if(Satellites > 3) {
b = true;
}
}
break;
}
return b;
}
private void setAnalyzedData(GpsData LogData) {
this.AnalyzedData = (LogData);
}
GpsData getAnalyzedData() {
return AnalyzedData;
}
}
java
New contributor
add a comment |
I have 2 Classes, the first one is only used as Container,
and the second Class Splits a GpsString, Converts it to suitable data types and returns the Container Class.
The String to analyze looks about like this:
("$GPGGA,120003.533,7926.184,N,02222.419,W,1,12,1.0,88.5,M,0.0,M,,*71")
Now i want to know if this is good practice in ObjectOriented Programming,
or if there is a better way for this Problem.
final class GpsData {
int Hours;
int Minutes;
double Seconds;
double Lattitude;
char DirectionLattitude; // North or South
double Longitude;
char DirectionLongitude; // North or South
int Quality;
int Satelites;
}
class ProtocolAnalyzer {
private GpsData AnalyzedData = new GpsData();
boolean AnalyzeData(String GpsData){
GpsData Gps = new GpsData();
int Attribute = 0;
StringBuilder WorkString = new StringBuilder();
int beginIndex = 0;
int length = GpsData.length();
for (int i = 0;i<=length;i++){
if(i == GpsData.length() ||
GpsData.charAt(i) == ','){
String Data = GpsData.substring(beginIndex,i);
boolean bool = ConvertData(Data,Attribute,Gps);
if(!bool) return false;
beginIndex = i + 1;
Attribute ++;
}
}
setAnalyzedData(Gps);
return true;
}
private boolean ConvertData(String Data,int Attribute,GpsData Gps){
boolean b = false;
switch (Attribute) {
case 0: // §GPGGA
if(Data.equals("$GPGGA")) {
b = true;
}
break;
case 1: //HHMMSS.sss
if(Data.length() == 10) {
b = true;
String split = Data.substring(0,2);
Gps.Hours = Integer.parseInt(split);
split = Data.substring(2,4);
Gps.Minutes = Integer.parseInt(split);
split = Data.substring(4,10);
Gps.Seconds = Double.parseDouble(split);
}
break;
case 2: //BBBB.BBBB
if (Data.length() != 0) {
b = true;
Gps.Lattitude = Double.parseDouble(Data);
}
break;
case 3: //b
if (Data.length() != 0) {
char DirLat = Data.charAt(0);
if (DirLat == 'N' ||
DirLat == 'S') {
b = true;
Gps.DirectionLattitude = DirLat;
}
}
break;
case 4: //LLLLL.LLLL
if (Data.length() != 0) {
b = true;
Gps.Longitude = Double.parseDouble(Data);
}
break;
case 5: //l
if (Data.length() != 0) {
char DirLong = Data.charAt(0);
if (DirLong == 'E' ||
DirLong == 'W') {
b = true;
Gps.DirectionLongitude = DirLong;
}
}
break;
case 6: //Q
if (Data.length() != 0) {
int Quality = Integer.parseInt(Data);
Gps.Quality = Quality;
if(Quality != 0) {
b = true;
}
}
break;
case 7: //NN
if (Data.length() != 0) {
int Satellites = Integer.parseInt(Data);
Gps.Satelites = Satellites;
if(Satellites > 3) {
b = true;
}
}
break;
}
return b;
}
private void setAnalyzedData(GpsData LogData) {
this.AnalyzedData = (LogData);
}
GpsData getAnalyzedData() {
return AnalyzedData;
}
}
java
New contributor
add a comment |
I have 2 Classes, the first one is only used as Container,
and the second Class Splits a GpsString, Converts it to suitable data types and returns the Container Class.
The String to analyze looks about like this:
("$GPGGA,120003.533,7926.184,N,02222.419,W,1,12,1.0,88.5,M,0.0,M,,*71")
Now i want to know if this is good practice in ObjectOriented Programming,
or if there is a better way for this Problem.
final class GpsData {
int Hours;
int Minutes;
double Seconds;
double Lattitude;
char DirectionLattitude; // North or South
double Longitude;
char DirectionLongitude; // North or South
int Quality;
int Satelites;
}
class ProtocolAnalyzer {
private GpsData AnalyzedData = new GpsData();
boolean AnalyzeData(String GpsData){
GpsData Gps = new GpsData();
int Attribute = 0;
StringBuilder WorkString = new StringBuilder();
int beginIndex = 0;
int length = GpsData.length();
for (int i = 0;i<=length;i++){
if(i == GpsData.length() ||
GpsData.charAt(i) == ','){
String Data = GpsData.substring(beginIndex,i);
boolean bool = ConvertData(Data,Attribute,Gps);
if(!bool) return false;
beginIndex = i + 1;
Attribute ++;
}
}
setAnalyzedData(Gps);
return true;
}
private boolean ConvertData(String Data,int Attribute,GpsData Gps){
boolean b = false;
switch (Attribute) {
case 0: // §GPGGA
if(Data.equals("$GPGGA")) {
b = true;
}
break;
case 1: //HHMMSS.sss
if(Data.length() == 10) {
b = true;
String split = Data.substring(0,2);
Gps.Hours = Integer.parseInt(split);
split = Data.substring(2,4);
Gps.Minutes = Integer.parseInt(split);
split = Data.substring(4,10);
Gps.Seconds = Double.parseDouble(split);
}
break;
case 2: //BBBB.BBBB
if (Data.length() != 0) {
b = true;
Gps.Lattitude = Double.parseDouble(Data);
}
break;
case 3: //b
if (Data.length() != 0) {
char DirLat = Data.charAt(0);
if (DirLat == 'N' ||
DirLat == 'S') {
b = true;
Gps.DirectionLattitude = DirLat;
}
}
break;
case 4: //LLLLL.LLLL
if (Data.length() != 0) {
b = true;
Gps.Longitude = Double.parseDouble(Data);
}
break;
case 5: //l
if (Data.length() != 0) {
char DirLong = Data.charAt(0);
if (DirLong == 'E' ||
DirLong == 'W') {
b = true;
Gps.DirectionLongitude = DirLong;
}
}
break;
case 6: //Q
if (Data.length() != 0) {
int Quality = Integer.parseInt(Data);
Gps.Quality = Quality;
if(Quality != 0) {
b = true;
}
}
break;
case 7: //NN
if (Data.length() != 0) {
int Satellites = Integer.parseInt(Data);
Gps.Satelites = Satellites;
if(Satellites > 3) {
b = true;
}
}
break;
}
return b;
}
private void setAnalyzedData(GpsData LogData) {
this.AnalyzedData = (LogData);
}
GpsData getAnalyzedData() {
return AnalyzedData;
}
}
java
New contributor
I have 2 Classes, the first one is only used as Container,
and the second Class Splits a GpsString, Converts it to suitable data types and returns the Container Class.
The String to analyze looks about like this:
("$GPGGA,120003.533,7926.184,N,02222.419,W,1,12,1.0,88.5,M,0.0,M,,*71")
Now i want to know if this is good practice in ObjectOriented Programming,
or if there is a better way for this Problem.
final class GpsData {
int Hours;
int Minutes;
double Seconds;
double Lattitude;
char DirectionLattitude; // North or South
double Longitude;
char DirectionLongitude; // North or South
int Quality;
int Satelites;
}
class ProtocolAnalyzer {
private GpsData AnalyzedData = new GpsData();
boolean AnalyzeData(String GpsData){
GpsData Gps = new GpsData();
int Attribute = 0;
StringBuilder WorkString = new StringBuilder();
int beginIndex = 0;
int length = GpsData.length();
for (int i = 0;i<=length;i++){
if(i == GpsData.length() ||
GpsData.charAt(i) == ','){
String Data = GpsData.substring(beginIndex,i);
boolean bool = ConvertData(Data,Attribute,Gps);
if(!bool) return false;
beginIndex = i + 1;
Attribute ++;
}
}
setAnalyzedData(Gps);
return true;
}
private boolean ConvertData(String Data,int Attribute,GpsData Gps){
boolean b = false;
switch (Attribute) {
case 0: // §GPGGA
if(Data.equals("$GPGGA")) {
b = true;
}
break;
case 1: //HHMMSS.sss
if(Data.length() == 10) {
b = true;
String split = Data.substring(0,2);
Gps.Hours = Integer.parseInt(split);
split = Data.substring(2,4);
Gps.Minutes = Integer.parseInt(split);
split = Data.substring(4,10);
Gps.Seconds = Double.parseDouble(split);
}
break;
case 2: //BBBB.BBBB
if (Data.length() != 0) {
b = true;
Gps.Lattitude = Double.parseDouble(Data);
}
break;
case 3: //b
if (Data.length() != 0) {
char DirLat = Data.charAt(0);
if (DirLat == 'N' ||
DirLat == 'S') {
b = true;
Gps.DirectionLattitude = DirLat;
}
}
break;
case 4: //LLLLL.LLLL
if (Data.length() != 0) {
b = true;
Gps.Longitude = Double.parseDouble(Data);
}
break;
case 5: //l
if (Data.length() != 0) {
char DirLong = Data.charAt(0);
if (DirLong == 'E' ||
DirLong == 'W') {
b = true;
Gps.DirectionLongitude = DirLong;
}
}
break;
case 6: //Q
if (Data.length() != 0) {
int Quality = Integer.parseInt(Data);
Gps.Quality = Quality;
if(Quality != 0) {
b = true;
}
}
break;
case 7: //NN
if (Data.length() != 0) {
int Satellites = Integer.parseInt(Data);
Gps.Satelites = Satellites;
if(Satellites > 3) {
b = true;
}
}
break;
}
return b;
}
private void setAnalyzedData(GpsData LogData) {
this.AnalyzedData = (LogData);
}
GpsData getAnalyzedData() {
return AnalyzedData;
}
}
java
java
New contributor
New contributor
edited 7 mins ago
Martin.Schloegelhofer
New contributor
asked 10 hours ago
Martin.SchloegelhoferMartin.Schloegelhofer
62
62
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First, please please please use Java naming conventions to aid readability. Methods and local variables like int Hours
should be in camelCase
and not CapitalCase
which is for classes. Statements like this:
boolean AnalyzeData(String GpsData){
are especially confusing. Is GpsData
referring to the local variable, or the class? That can get very nasty very quickly. Please fix this before you do anything else.
final class GpsData {
This does not do what I suspect you think it does. A final
declaration for a class just means it can't be further extended. Its data can still be modified. To properly encapsulate the properties of GpsData
you should declare each of those properties as private
and provide public
constructors, setters, and getters as appropriate.
New contributor
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Martin.Schloegelhofer is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211062%2fsplitting-a-string-and-returning-converted-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, please please please use Java naming conventions to aid readability. Methods and local variables like int Hours
should be in camelCase
and not CapitalCase
which is for classes. Statements like this:
boolean AnalyzeData(String GpsData){
are especially confusing. Is GpsData
referring to the local variable, or the class? That can get very nasty very quickly. Please fix this before you do anything else.
final class GpsData {
This does not do what I suspect you think it does. A final
declaration for a class just means it can't be further extended. Its data can still be modified. To properly encapsulate the properties of GpsData
you should declare each of those properties as private
and provide public
constructors, setters, and getters as appropriate.
New contributor
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
add a comment |
First, please please please use Java naming conventions to aid readability. Methods and local variables like int Hours
should be in camelCase
and not CapitalCase
which is for classes. Statements like this:
boolean AnalyzeData(String GpsData){
are especially confusing. Is GpsData
referring to the local variable, or the class? That can get very nasty very quickly. Please fix this before you do anything else.
final class GpsData {
This does not do what I suspect you think it does. A final
declaration for a class just means it can't be further extended. Its data can still be modified. To properly encapsulate the properties of GpsData
you should declare each of those properties as private
and provide public
constructors, setters, and getters as appropriate.
New contributor
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
add a comment |
First, please please please use Java naming conventions to aid readability. Methods and local variables like int Hours
should be in camelCase
and not CapitalCase
which is for classes. Statements like this:
boolean AnalyzeData(String GpsData){
are especially confusing. Is GpsData
referring to the local variable, or the class? That can get very nasty very quickly. Please fix this before you do anything else.
final class GpsData {
This does not do what I suspect you think it does. A final
declaration for a class just means it can't be further extended. Its data can still be modified. To properly encapsulate the properties of GpsData
you should declare each of those properties as private
and provide public
constructors, setters, and getters as appropriate.
New contributor
First, please please please use Java naming conventions to aid readability. Methods and local variables like int Hours
should be in camelCase
and not CapitalCase
which is for classes. Statements like this:
boolean AnalyzeData(String GpsData){
are especially confusing. Is GpsData
referring to the local variable, or the class? That can get very nasty very quickly. Please fix this before you do anything else.
final class GpsData {
This does not do what I suspect you think it does. A final
declaration for a class just means it can't be further extended. Its data can still be modified. To properly encapsulate the properties of GpsData
you should declare each of those properties as private
and provide public
constructors, setters, and getters as appropriate.
New contributor
New contributor
answered 10 hours ago
Stalemate Of TuningStalemate Of Tuning
1113
1113
New contributor
New contributor
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
add a comment |
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
Ok i understand, i will use the naming conventions in the future. But about the setters, getters and constructors, i want to get the whole structure with one getter not multiple ones.
– Martin.Schloegelhofer
38 mins ago
add a comment |
Martin.Schloegelhofer is a new contributor. Be nice, and check out our Code of Conduct.
Martin.Schloegelhofer is a new contributor. Be nice, and check out our Code of Conduct.
Martin.Schloegelhofer is a new contributor. Be nice, and check out our Code of Conduct.
Martin.Schloegelhofer is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211062%2fsplitting-a-string-and-returning-converted-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown